Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / include / ipxe / infiniband.h
1 #ifndef _IPXE_INFINIBAND_H
2 #define _IPXE_INFINIBAND_H
3
4 /** @file
5  *
6  * Infiniband protocol
7  *
8  */
9
10 FILE_LICENCE ( GPL2_OR_LATER );
11
12 #include <stdint.h>
13 #include <ipxe/refcnt.h>
14 #include <ipxe/device.h>
15 #include <ipxe/tables.h>
16 #include <ipxe/ib_packet.h>
17 #include <ipxe/ib_mad.h>
18
19 /** Subnet management interface QPN */
20 #define IB_QPN_SMI 0
21
22 /** Subnet management interface queue key */
23 #define IB_QKEY_SMI 0
24
25 /** General service interface QPN */
26 #define IB_QPN_GSI 1
27
28 /** General service interface queue key */
29 #define IB_QKEY_GSI 0x80010000UL
30
31 /** Broadcast QPN */
32 #define IB_QPN_BROADCAST 0xffffffUL
33
34 /** QPN mask */
35 #define IB_QPN_MASK 0xffffffUL
36
37 /** Default Infiniband partition key */
38 #define IB_PKEY_DEFAULT 0xffff
39
40 /** Infiniband partition key full membership flag */
41 #define IB_PKEY_FULL 0x8000
42
43 /**
44  * Maximum payload size
45  *
46  * This is currently hard-coded in various places (drivers, subnet
47  * management agent, etc.) to 2048.
48  */
49 #define IB_MAX_PAYLOAD_SIZE 2048
50
51 struct ib_device;
52 struct ib_queue_pair;
53 struct ib_address_vector;
54 struct ib_completion_queue;
55 struct ib_mad_interface;
56
57 /** Infiniband transmission rates */
58 enum ib_rate {
59         IB_RATE_2_5 = 2,
60         IB_RATE_10 = 3,
61         IB_RATE_30 = 4,
62         IB_RATE_5 = 5,
63         IB_RATE_20 = 6,
64         IB_RATE_40 = 7,
65         IB_RATE_60 = 8,
66         IB_RATE_80 = 9,
67         IB_RATE_120 = 10,
68 };
69
70 /** An Infiniband Address Vector */
71 struct ib_address_vector {
72         /** Queue Pair Number */
73         unsigned long qpn;
74         /** Queue key
75          *
76          * Not specified for received packets.
77          */
78         unsigned long qkey;
79         /** Local ID */
80         unsigned int lid;
81         /** Rate
82          *
83          * Not specified for received packets.
84          */
85         enum ib_rate rate;
86         /** Service level */
87         unsigned int sl;
88         /** GID is present */
89         unsigned int gid_present;
90         /** GID, if present */
91         union ib_gid gid;
92         /** VLAN is present */
93         unsigned int vlan_present;
94         /** VLAN, if present */
95         unsigned int vlan;
96 };
97
98 /** An Infiniband Work Queue */
99 struct ib_work_queue {
100         /** Containing queue pair */
101         struct ib_queue_pair *qp;
102         /** "Is a send queue" flag */
103         int is_send;
104         /** Associated completion queue */
105         struct ib_completion_queue *cq;
106         /** List of work queues on this completion queue */
107         struct list_head list;
108         /** Packet sequence number */
109         uint32_t psn;
110         /** Number of work queue entries */
111         unsigned int num_wqes;
112         /** Number of occupied work queue entries */
113         unsigned int fill;
114         /** Next work queue entry index
115          *
116          * This is the index of the next entry to be filled (i.e. the
117          * first empty entry).  This value is not bounded by num_wqes;
118          * users must logical-AND with (num_wqes-1) to generate an
119          * array index.
120          */
121         unsigned long next_idx;
122         /** I/O buffers assigned to work queue */
123         struct io_buffer **iobufs;
124         /** Driver private data */
125         void *drv_priv;
126 };
127
128 /** An Infiniband multicast GID */
129 struct ib_multicast_gid {
130         /** List of multicast GIDs on this QP */
131         struct list_head list;
132         /** Multicast GID */
133         union ib_gid gid;
134 };
135
136 /** An Infiniband queue pair type */
137 enum ib_queue_pair_type {
138         IB_QPT_SMI,
139         IB_QPT_GSI,
140         IB_QPT_UD,
141         IB_QPT_RC,
142         IB_QPT_ETH,
143 };
144
145 /** Infiniband queue pair operations */
146 struct ib_queue_pair_operations {
147         /** Allocate receive I/O buffer
148          *
149          * @v len               Maximum receive length
150          * @ret iobuf           I/O buffer (or NULL if out of memory)
151          */
152         struct io_buffer * ( * alloc_iob ) ( size_t len );
153 };
154
155 /** An Infiniband Queue Pair */
156 struct ib_queue_pair {
157         /** Containing Infiniband device */
158         struct ib_device *ibdev;
159         /** List of queue pairs on this Infiniband device */
160         struct list_head list;
161         /** Queue pair number */
162         unsigned long qpn;
163         /** Externally-visible queue pair number
164          *
165          * This may differ from the real queue pair number (e.g. when
166          * the HCA cannot use the management QPNs 0 and 1 as hardware
167          * QPNs and needs to remap them).
168          */
169         unsigned long ext_qpn;
170         /** Queue pair type */
171         enum ib_queue_pair_type type;
172         /** Queue key */
173         unsigned long qkey;
174         /** Send queue */
175         struct ib_work_queue send;
176         /** Receive queue */
177         struct ib_work_queue recv;
178         /** List of multicast GIDs */
179         struct list_head mgids;
180         /** Address vector */
181         struct ib_address_vector av;
182         /** Queue pair operations */
183         struct ib_queue_pair_operations *op;
184         /** Driver private data */
185         void *drv_priv;
186         /** Queue owner private data */
187         void *owner_priv;
188 };
189
190 /** Infiniband completion queue operations */
191 struct ib_completion_queue_operations {
192         /**
193          * Complete Send WQE
194          *
195          * @v ibdev             Infiniband device
196          * @v qp                Queue pair
197          * @v iobuf             I/O buffer
198          * @v rc                Completion status code
199          */
200         void ( * complete_send ) ( struct ib_device *ibdev,
201                                    struct ib_queue_pair *qp,
202                                    struct io_buffer *iobuf, int rc );
203         /**
204          * Complete Receive WQE
205          *
206          * @v ibdev             Infiniband device
207          * @v qp                Queue pair
208          * @v dest              Destination address vector, or NULL
209          * @v source            Source address vector, or NULL
210          * @v iobuf             I/O buffer
211          * @v rc                Completion status code
212          */
213         void ( * complete_recv ) ( struct ib_device *ibdev,
214                                    struct ib_queue_pair *qp,
215                                    struct ib_address_vector *dest,
216                                    struct ib_address_vector *source,
217                                    struct io_buffer *iobuf, int rc );
218 };
219
220 /** An Infiniband Completion Queue */
221 struct ib_completion_queue {
222         /** Containing Infiniband device */
223         struct ib_device *ibdev;
224         /** List of completion queues on this Infiniband device */
225         struct list_head list;
226         /** Completion queue number */
227         unsigned long cqn;
228         /** Number of completion queue entries */
229         unsigned int num_cqes;
230         /** Next completion queue entry index
231          *
232          * This is the index of the next entry to be filled (i.e. the
233          * first empty entry).  This value is not bounded by num_wqes;
234          * users must logical-AND with (num_wqes-1) to generate an
235          * array index.
236          */
237         unsigned long next_idx;
238         /** List of work queues completing to this queue */
239         struct list_head work_queues;
240         /** Completion queue operations */
241         struct ib_completion_queue_operations *op;
242         /** Driver private data */
243         void *drv_priv;
244 };
245
246 /**
247  * Infiniband device operations
248  *
249  * These represent a subset of the Infiniband Verbs.
250  */
251 struct ib_device_operations {
252         /** Create completion queue
253          *
254          * @v ibdev             Infiniband device
255          * @v cq                Completion queue
256          * @ret rc              Return status code
257          */
258         int ( * create_cq ) ( struct ib_device *ibdev,
259                               struct ib_completion_queue *cq );
260         /** Destroy completion queue
261          *
262          * @v ibdev             Infiniband device
263          * @v cq                Completion queue
264          */
265         void ( * destroy_cq ) ( struct ib_device *ibdev,
266                                 struct ib_completion_queue *cq );
267         /** Create queue pair
268          *
269          * @v ibdev             Infiniband device
270          * @v qp                Queue pair
271          * @ret rc              Return status code
272          */
273         int ( * create_qp ) ( struct ib_device *ibdev,
274                               struct ib_queue_pair *qp );
275         /** Modify queue pair
276          *
277          * @v ibdev             Infiniband device
278          * @v qp                Queue pair
279          * @ret rc              Return status code
280          */
281         int ( * modify_qp ) ( struct ib_device *ibdev,
282                               struct ib_queue_pair *qp );
283         /** Destroy queue pair
284          *
285          * @v ibdev             Infiniband device
286          * @v qp                Queue pair
287          */
288         void ( * destroy_qp ) ( struct ib_device *ibdev,
289                                 struct ib_queue_pair *qp );
290         /** Post send work queue entry
291          *
292          * @v ibdev             Infiniband device
293          * @v qp                Queue pair
294          * @v dest              Destination address vector
295          * @v iobuf             I/O buffer
296          * @ret rc              Return status code
297          *
298          * If this method returns success, the I/O buffer remains
299          * owned by the queue pair.  If this method returns failure,
300          * the I/O buffer is immediately released; the failure is
301          * interpreted as "failure to enqueue buffer".
302          */
303         int ( * post_send ) ( struct ib_device *ibdev,
304                               struct ib_queue_pair *qp,
305                               struct ib_address_vector *dest,
306                               struct io_buffer *iobuf );
307         /** Post receive work queue entry
308          *
309          * @v ibdev             Infiniband device
310          * @v qp                Queue pair
311          * @v iobuf             I/O buffer
312          * @ret rc              Return status code
313          *
314          * If this method returns success, the I/O buffer remains
315          * owned by the queue pair.  If this method returns failure,
316          * the I/O buffer is immediately released; the failure is
317          * interpreted as "failure to enqueue buffer".
318          */
319         int ( * post_recv ) ( struct ib_device *ibdev,
320                               struct ib_queue_pair *qp,
321                               struct io_buffer *iobuf );
322         /** Poll completion queue
323          *
324          * @v ibdev             Infiniband device
325          * @v cq                Completion queue
326          *
327          * The relevant completion handler (specified at completion
328          * queue creation time) takes ownership of the I/O buffer.
329          */
330         void ( * poll_cq ) ( struct ib_device *ibdev,
331                              struct ib_completion_queue *cq );
332         /**
333          * Poll event queue
334          *
335          * @v ibdev             Infiniband device
336          */
337         void ( * poll_eq ) ( struct ib_device *ibdev );
338         /**
339          * Open port
340          *
341          * @v ibdev             Infiniband device
342          * @ret rc              Return status code
343          */
344         int ( * open ) ( struct ib_device *ibdev );
345         /**
346          * Close port
347          *
348          * @v ibdev             Infiniband device
349          */
350         void ( * close ) ( struct ib_device *ibdev );
351         /** Attach to multicast group
352          *
353          * @v ibdev             Infiniband device
354          * @v qp                Queue pair
355          * @v gid               Multicast GID
356          * @ret rc              Return status code
357          */
358         int ( * mcast_attach ) ( struct ib_device *ibdev,
359                                  struct ib_queue_pair *qp,
360                                  union ib_gid *gid );
361         /** Detach from multicast group
362          *
363          * @v ibdev             Infiniband device
364          * @v qp                Queue pair
365          * @v gid               Multicast GID
366          */
367         void ( * mcast_detach ) ( struct ib_device *ibdev,
368                                   struct ib_queue_pair *qp,
369                                   union ib_gid *gid );
370         /** Set port information
371          *
372          * @v ibdev             Infiniband device
373          * @v mad               Set port information MAD
374          *
375          * This method is required only by adapters that do not have
376          * an embedded SMA.
377          */
378         int ( * set_port_info ) ( struct ib_device *ibdev, union ib_mad *mad );
379         /** Set partition key table
380          *
381          * @v ibdev             Infiniband device
382          * @v mad               Set partition key table MAD
383          *
384          * This method is required only by adapters that do not have
385          * an embedded SMA.
386          */
387         int ( * set_pkey_table ) ( struct ib_device *ibdev,
388                                    union ib_mad *mad );
389 };
390
391 /** An Infiniband device */
392 struct ib_device {
393         /** Reference counter */
394         struct refcnt refcnt;
395         /** List of Infiniband devices */
396         struct list_head list;
397         /** List of open Infiniband devices */
398         struct list_head open_list;
399         /** Underlying device */
400         struct device *dev;
401         /** List of completion queues */
402         struct list_head cqs;
403         /** List of queue pairs */
404         struct list_head qps;
405         /** Infiniband operations */
406         struct ib_device_operations *op;
407         /** Port number */
408         unsigned int port;
409         /** Port open request counter */
410         unsigned int open_count;
411
412         /** Port state */
413         uint8_t port_state;
414         /** Link width supported */
415         uint8_t link_width_supported;
416         /** Link width enabled */
417         uint8_t link_width_enabled;
418         /** Link width active */
419         uint8_t link_width_active;
420         /** Link speed supported */
421         uint8_t link_speed_supported;
422         /** Link speed enabled */
423         uint8_t link_speed_enabled;
424         /** Link speed active */
425         uint8_t link_speed_active;
426         /** Node GUID */
427         union ib_guid node_guid;
428         /** Port GID (comprising GID prefix and port GUID) */
429         union ib_gid gid;
430         /** Port LID */
431         uint16_t lid;
432         /** Subnet manager LID */
433         uint16_t sm_lid;
434         /** Subnet manager SL */
435         uint8_t sm_sl;
436         /** Partition key */
437         uint16_t pkey;
438
439         /** RDMA key
440          *
441          * This is a single key allowing unrestricted access to
442          * memory.
443          */
444         uint32_t rdma_key;
445
446         /** Subnet management interface */
447         struct ib_mad_interface *smi;
448         /** General services interface */
449         struct ib_mad_interface *gsi;
450
451         /** Driver private data */
452         void *drv_priv;
453         /** Owner private data */
454         void *owner_priv;
455 };
456
457 /** An Infiniband upper-layer driver */
458 struct ib_driver {
459         /** Name */
460         const char *name;
461         /** Probe device
462          *
463          * @v ibdev             Infiniband device
464          * @ret rc              Return status code
465          */
466         int ( * probe ) ( struct ib_device *ibdev );
467         /** Notify of device or link state change
468          *
469          * @v ibdev             Infiniband device
470          */
471         void ( * notify ) ( struct ib_device *ibdev );
472         /** Remove device
473          *
474          * @v ibdev             Infiniband device
475          */
476         void ( * remove ) ( struct ib_device *ibdev );
477 };
478
479 /** Infiniband driver table */
480 #define IB_DRIVERS __table ( struct ib_driver, "ib_drivers" )
481
482 /** Declare an Infiniband driver */
483 #define __ib_driver __table_entry ( IB_DRIVERS, 01 )
484
485 extern struct ib_completion_queue *
486 ib_create_cq ( struct ib_device *ibdev, unsigned int num_cqes,
487                struct ib_completion_queue_operations *op );
488 extern void ib_destroy_cq ( struct ib_device *ibdev,
489                             struct ib_completion_queue *cq );
490 extern void ib_poll_cq ( struct ib_device *ibdev,
491                          struct ib_completion_queue *cq );
492 extern struct ib_queue_pair *
493 ib_create_qp ( struct ib_device *ibdev, enum ib_queue_pair_type type,
494                unsigned int num_send_wqes, struct ib_completion_queue *send_cq,
495                unsigned int num_recv_wqes, struct ib_completion_queue *recv_cq,
496                struct ib_queue_pair_operations *op );
497 extern int ib_modify_qp ( struct ib_device *ibdev, struct ib_queue_pair *qp );
498 extern void ib_destroy_qp ( struct ib_device *ibdev,
499                             struct ib_queue_pair *qp );
500 extern struct ib_queue_pair * ib_find_qp_qpn ( struct ib_device *ibdev,
501                                                unsigned long qpn );
502 extern struct ib_queue_pair * ib_find_qp_mgid ( struct ib_device *ibdev,
503                                                 union ib_gid *gid );
504 extern struct ib_work_queue * ib_find_wq ( struct ib_completion_queue *cq,
505                                            unsigned long qpn, int is_send );
506 extern int ib_post_send ( struct ib_device *ibdev, struct ib_queue_pair *qp,
507                           struct ib_address_vector *dest,
508                           struct io_buffer *iobuf );
509 extern int ib_post_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp,
510                           struct io_buffer *iobuf );
511 extern void ib_complete_send ( struct ib_device *ibdev,
512                                struct ib_queue_pair *qp,
513                                struct io_buffer *iobuf, int rc );
514 extern void ib_complete_recv ( struct ib_device *ibdev,
515                                struct ib_queue_pair *qp,
516                                struct ib_address_vector *dest,
517                                struct ib_address_vector *source,
518                                struct io_buffer *iobuf, int rc );
519 extern void ib_refill_recv ( struct ib_device *ibdev,
520                              struct ib_queue_pair *qp );
521 extern int ib_open ( struct ib_device *ibdev );
522 extern void ib_close ( struct ib_device *ibdev );
523 extern int ib_link_rc ( struct ib_device *ibdev );
524 extern int ib_mcast_attach ( struct ib_device *ibdev, struct ib_queue_pair *qp,
525                              union ib_gid *gid );
526 extern void ib_mcast_detach ( struct ib_device *ibdev,
527                               struct ib_queue_pair *qp, union ib_gid *gid );
528 extern int ib_count_ports ( struct ib_device *ibdev );
529 extern int ib_set_port_info ( struct ib_device *ibdev, union ib_mad *mad );
530 extern int ib_set_pkey_table ( struct ib_device *ibdev, union ib_mad *mad );
531 extern struct ib_device * alloc_ibdev ( size_t priv_size );
532 extern int register_ibdev ( struct ib_device *ibdev );
533 extern void unregister_ibdev ( struct ib_device *ibdev );
534 extern struct ib_device * find_ibdev ( union ib_gid *gid );
535 extern struct ib_device * last_opened_ibdev ( void );
536 extern void ib_link_state_changed ( struct ib_device *ibdev );
537 extern void ib_poll_eq ( struct ib_device *ibdev );
538 extern struct list_head ib_devices;
539
540 /** Iterate over all network devices */
541 #define for_each_ibdev( ibdev ) \
542         list_for_each_entry ( (ibdev), &ib_devices, list )
543
544 /**
545  * Check link state of Infiniband device
546  *
547  * @v ibdev             Infiniband device
548  * @ret link_up         Link is up
549  */
550 static inline __always_inline int
551 ib_link_ok ( struct ib_device *ibdev ) {
552         return ( ibdev->port_state == IB_PORT_STATE_ACTIVE );
553 }
554
555 /**
556  * Check whether or not Infiniband device is open
557  *
558  * @v ibdev             Infiniband device
559  * @v is_open           Infiniband device is open
560  */
561 static inline __attribute__ (( always_inline )) int
562 ib_is_open ( struct ib_device *ibdev ) {
563         return ( ibdev->open_count > 0 );
564 }
565
566 /**
567  * Get reference to Infiniband device
568  *
569  * @v ibdev             Infiniband device
570  * @ret ibdev           Infiniband device
571  */
572 static inline __always_inline struct ib_device *
573 ibdev_get ( struct ib_device *ibdev ) {
574         ref_get ( &ibdev->refcnt );
575         return ibdev;
576 }
577
578 /**
579  * Drop reference to Infiniband device
580  *
581  * @v ibdev             Infiniband device
582  */
583 static inline __always_inline void
584 ibdev_put ( struct ib_device *ibdev ) {
585         ref_put ( &ibdev->refcnt );
586 }
587
588 /**
589  * Set Infiniband work queue driver-private data
590  *
591  * @v wq                Work queue
592  * @v priv              Private data
593  */
594 static inline __always_inline void
595 ib_wq_set_drvdata ( struct ib_work_queue *wq, void *priv ) {
596         wq->drv_priv = priv;
597 }
598
599 /**
600  * Get Infiniband work queue driver-private data
601  *
602  * @v wq                Work queue
603  * @ret priv            Private data
604  */
605 static inline __always_inline void *
606 ib_wq_get_drvdata ( struct ib_work_queue *wq ) {
607         return wq->drv_priv;
608 }
609
610 /**
611  * Set Infiniband queue pair driver-private data
612  *
613  * @v qp                Queue pair
614  * @v priv              Private data
615  */
616 static inline __always_inline void
617 ib_qp_set_drvdata ( struct ib_queue_pair *qp, void *priv ) {
618         qp->drv_priv = priv;
619 }
620
621 /**
622  * Get Infiniband queue pair driver-private data
623  *
624  * @v qp                Queue pair
625  * @ret priv            Private data
626  */
627 static inline __always_inline void *
628 ib_qp_get_drvdata ( struct ib_queue_pair *qp ) {
629         return qp->drv_priv;
630 }
631
632 /**
633  * Set Infiniband queue pair owner-private data
634  *
635  * @v qp                Queue pair
636  * @v priv              Private data
637  */
638 static inline __always_inline void
639 ib_qp_set_ownerdata ( struct ib_queue_pair *qp, void *priv ) {
640         qp->owner_priv = priv;
641 }
642
643 /**
644  * Get Infiniband queue pair owner-private data
645  *
646  * @v qp                Queue pair
647  * @ret priv            Private data
648  */
649 static inline __always_inline void *
650 ib_qp_get_ownerdata ( struct ib_queue_pair *qp ) {
651         return qp->owner_priv;
652 }
653
654 /**
655  * Set Infiniband completion queue driver-private data
656  *
657  * @v cq                Completion queue
658  * @v priv              Private data
659  */
660 static inline __always_inline void
661 ib_cq_set_drvdata ( struct ib_completion_queue *cq, void *priv ) {
662         cq->drv_priv = priv;
663 }
664
665 /**
666  * Get Infiniband completion queue driver-private data
667  *
668  * @v cq                Completion queue
669  * @ret priv            Private data
670  */
671 static inline __always_inline void *
672 ib_cq_get_drvdata ( struct ib_completion_queue *cq ) {
673         return cq->drv_priv;
674 }
675
676 /**
677  * Set Infiniband device driver-private data
678  *
679  * @v ibdev             Infiniband device
680  * @v priv              Private data
681  */
682 static inline __always_inline void
683 ib_set_drvdata ( struct ib_device *ibdev, void *priv ) {
684         ibdev->drv_priv = priv;
685 }
686
687 /**
688  * Get Infiniband device driver-private data
689  *
690  * @v ibdev             Infiniband device
691  * @ret priv            Private data
692  */
693 static inline __always_inline void *
694 ib_get_drvdata ( struct ib_device *ibdev ) {
695         return ibdev->drv_priv;
696 }
697
698 /**
699  * Set Infiniband device owner-private data
700  *
701  * @v ibdev             Infiniband device
702  * @v priv              Private data
703  */
704 static inline __always_inline void
705 ib_set_ownerdata ( struct ib_device *ibdev, void *priv ) {
706         ibdev->owner_priv = priv;
707 }
708
709 /**
710  * Get Infiniband device owner-private data
711  *
712  * @v ibdev             Infiniband device
713  * @ret priv            Private data
714  */
715 static inline __always_inline void *
716 ib_get_ownerdata ( struct ib_device *ibdev ) {
717         return ibdev->owner_priv;
718 }
719
720 #endif /* _IPXE_INFINIBAND_H */