[infiniband] Assign names to Infiniband devices for debug messages
[ipxe.git] / 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_OR_UBDL );
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 /** Maximum length of an Infiniband device name */
392 #define IBDEV_NAME_LEN 8
393
394 /** An Infiniband device */
395 struct ib_device {
396 /** Reference counter */
397 struct refcnt refcnt;
398 /** List of Infiniband devices */
399 struct list_head list;
400 /** List of open Infiniband devices */
401 struct list_head open_list;
402 /** Index of this Infiniband device */
403 unsigned int index;
404 /** Name of this Infiniband device */
405 char name[IBDEV_NAME_LEN];
406 /** Underlying device */
407 struct device *dev;
408 /** List of completion queues */
409 struct list_head cqs;
410 /** List of queue pairs */
411 struct list_head qps;
412 /** Infiniband operations */
413 struct ib_device_operations *op;
414 /** Port number */
415 unsigned int port;
416 /** Port open request counter */
417 unsigned int open_count;
418
419 /** Port state */
420 uint8_t port_state;
421 /** Link width supported */
422 uint8_t link_width_supported;
423 /** Link width enabled */
424 uint8_t link_width_enabled;
425 /** Link width active */
426 uint8_t link_width_active;
427 /** Link speed supported */
428 uint8_t link_speed_supported;
429 /** Link speed enabled */
430 uint8_t link_speed_enabled;
431 /** Link speed active */
432 uint8_t link_speed_active;
433 /** Node GUID */
434 union ib_guid node_guid;
435 /** Port GID (comprising GID prefix and port GUID) */
436 union ib_gid gid;
437 /** Port LID */
438 uint16_t lid;
439 /** Subnet manager LID */
440 uint16_t sm_lid;
441 /** Subnet manager SL */
442 uint8_t sm_sl;
443 /** Partition key */
444 uint16_t pkey;
445
446 /** RDMA key
447 *
448 * This is a single key allowing unrestricted access to
449 * memory.
450 */
451 uint32_t rdma_key;
452
453 /** Subnet management interface */
454 struct ib_mad_interface *smi;
455 /** General services interface */
456 struct ib_mad_interface *gsi;
457
458 /** Driver private data */
459 void *drv_priv;
460 };
461
462 /** An Infiniband upper-layer driver */
463 struct ib_driver {
464 /** Name */
465 const char *name;
466 /** Probe device
467 *
468 * @v ibdev Infiniband device
469 * @ret rc Return status code
470 */
471 int ( * probe ) ( struct ib_device *ibdev );
472 /** Notify of device or link state change
473 *
474 * @v ibdev Infiniband device
475 */
476 void ( * notify ) ( struct ib_device *ibdev );
477 /** Remove device
478 *
479 * @v ibdev Infiniband device
480 */
481 void ( * remove ) ( struct ib_device *ibdev );
482 };
483
484 /** Infiniband driver table */
485 #define IB_DRIVERS __table ( struct ib_driver, "ib_drivers" )
486
487 /** Declare an Infiniband driver */
488 #define __ib_driver __table_entry ( IB_DRIVERS, 01 )
489
490 extern struct ib_completion_queue *
491 ib_create_cq ( struct ib_device *ibdev, unsigned int num_cqes,
492 struct ib_completion_queue_operations *op );
493 extern void ib_destroy_cq ( struct ib_device *ibdev,
494 struct ib_completion_queue *cq );
495 extern void ib_poll_cq ( struct ib_device *ibdev,
496 struct ib_completion_queue *cq );
497 extern struct ib_queue_pair *
498 ib_create_qp ( struct ib_device *ibdev, enum ib_queue_pair_type type,
499 unsigned int num_send_wqes, struct ib_completion_queue *send_cq,
500 unsigned int num_recv_wqes, struct ib_completion_queue *recv_cq,
501 struct ib_queue_pair_operations *op );
502 extern int ib_modify_qp ( struct ib_device *ibdev, struct ib_queue_pair *qp );
503 extern void ib_destroy_qp ( struct ib_device *ibdev,
504 struct ib_queue_pair *qp );
505 extern struct ib_queue_pair * ib_find_qp_qpn ( struct ib_device *ibdev,
506 unsigned long qpn );
507 extern struct ib_queue_pair * ib_find_qp_mgid ( struct ib_device *ibdev,
508 union ib_gid *gid );
509 extern struct ib_work_queue * ib_find_wq ( struct ib_completion_queue *cq,
510 unsigned long qpn, int is_send );
511 extern int ib_post_send ( struct ib_device *ibdev, struct ib_queue_pair *qp,
512 struct ib_address_vector *dest,
513 struct io_buffer *iobuf );
514 extern int ib_post_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp,
515 struct io_buffer *iobuf );
516 extern void ib_complete_send ( struct ib_device *ibdev,
517 struct ib_queue_pair *qp,
518 struct io_buffer *iobuf, int rc );
519 extern void ib_complete_recv ( struct ib_device *ibdev,
520 struct ib_queue_pair *qp,
521 struct ib_address_vector *dest,
522 struct ib_address_vector *source,
523 struct io_buffer *iobuf, int rc );
524 extern void ib_refill_recv ( struct ib_device *ibdev,
525 struct ib_queue_pair *qp );
526 extern int ib_open ( struct ib_device *ibdev );
527 extern void ib_close ( struct ib_device *ibdev );
528 extern int ib_link_rc ( struct ib_device *ibdev );
529 extern int ib_mcast_attach ( struct ib_device *ibdev, struct ib_queue_pair *qp,
530 union ib_gid *gid );
531 extern void ib_mcast_detach ( struct ib_device *ibdev,
532 struct ib_queue_pair *qp, union ib_gid *gid );
533 extern int ib_count_ports ( struct ib_device *ibdev );
534 extern int ib_set_port_info ( struct ib_device *ibdev, union ib_mad *mad );
535 extern int ib_set_pkey_table ( struct ib_device *ibdev, union ib_mad *mad );
536 extern struct ib_device * alloc_ibdev ( size_t priv_size );
537 extern int register_ibdev ( struct ib_device *ibdev );
538 extern void unregister_ibdev ( struct ib_device *ibdev );
539 extern struct ib_device * find_ibdev ( union ib_gid *gid );
540 extern struct ib_device * last_opened_ibdev ( void );
541 extern void ib_link_state_changed ( struct ib_device *ibdev );
542 extern void ib_poll_eq ( struct ib_device *ibdev );
543 extern struct list_head ib_devices;
544
545 /** Iterate over all network devices */
546 #define for_each_ibdev( ibdev ) \
547 list_for_each_entry ( (ibdev), &ib_devices, list )
548
549 /**
550 * Check link state of Infiniband device
551 *
552 * @v ibdev Infiniband device
553 * @ret link_up Link is up
554 */
555 static inline __always_inline int
556 ib_link_ok ( struct ib_device *ibdev ) {
557 return ( ibdev->port_state == IB_PORT_STATE_ACTIVE );
558 }
559
560 /**
561 * Check whether or not Infiniband device is open
562 *
563 * @v ibdev Infiniband device
564 * @v is_open Infiniband device is open
565 */
566 static inline __attribute__ (( always_inline )) int
567 ib_is_open ( struct ib_device *ibdev ) {
568 return ( ibdev->open_count > 0 );
569 }
570
571 /**
572 * Get reference to Infiniband device
573 *
574 * @v ibdev Infiniband device
575 * @ret ibdev Infiniband device
576 */
577 static inline __always_inline struct ib_device *
578 ibdev_get ( struct ib_device *ibdev ) {
579 ref_get ( &ibdev->refcnt );
580 return ibdev;
581 }
582
583 /**
584 * Drop reference to Infiniband device
585 *
586 * @v ibdev Infiniband device
587 */
588 static inline __always_inline void
589 ibdev_put ( struct ib_device *ibdev ) {
590 ref_put ( &ibdev->refcnt );
591 }
592
593 /**
594 * Set Infiniband work queue driver-private data
595 *
596 * @v wq Work queue
597 * @v priv Private data
598 */
599 static inline __always_inline void
600 ib_wq_set_drvdata ( struct ib_work_queue *wq, void *priv ) {
601 wq->drv_priv = priv;
602 }
603
604 /**
605 * Get Infiniband work queue driver-private data
606 *
607 * @v wq Work queue
608 * @ret priv Private data
609 */
610 static inline __always_inline void *
611 ib_wq_get_drvdata ( struct ib_work_queue *wq ) {
612 return wq->drv_priv;
613 }
614
615 /**
616 * Set Infiniband queue pair driver-private data
617 *
618 * @v qp Queue pair
619 * @v priv Private data
620 */
621 static inline __always_inline void
622 ib_qp_set_drvdata ( struct ib_queue_pair *qp, void *priv ) {
623 qp->drv_priv = priv;
624 }
625
626 /**
627 * Get Infiniband queue pair driver-private data
628 *
629 * @v qp Queue pair
630 * @ret priv Private data
631 */
632 static inline __always_inline void *
633 ib_qp_get_drvdata ( struct ib_queue_pair *qp ) {
634 return qp->drv_priv;
635 }
636
637 /**
638 * Set Infiniband queue pair owner-private data
639 *
640 * @v qp Queue pair
641 * @v priv Private data
642 */
643 static inline __always_inline void
644 ib_qp_set_ownerdata ( struct ib_queue_pair *qp, void *priv ) {
645 qp->owner_priv = priv;
646 }
647
648 /**
649 * Get Infiniband queue pair owner-private data
650 *
651 * @v qp Queue pair
652 * @ret priv Private data
653 */
654 static inline __always_inline void *
655 ib_qp_get_ownerdata ( struct ib_queue_pair *qp ) {
656 return qp->owner_priv;
657 }
658
659 /**
660 * Set Infiniband completion queue driver-private data
661 *
662 * @v cq Completion queue
663 * @v priv Private data
664 */
665 static inline __always_inline void
666 ib_cq_set_drvdata ( struct ib_completion_queue *cq, void *priv ) {
667 cq->drv_priv = priv;
668 }
669
670 /**
671 * Get Infiniband completion queue driver-private data
672 *
673 * @v cq Completion queue
674 * @ret priv Private data
675 */
676 static inline __always_inline void *
677 ib_cq_get_drvdata ( struct ib_completion_queue *cq ) {
678 return cq->drv_priv;
679 }
680
681 /**
682 * Set Infiniband device driver-private data
683 *
684 * @v ibdev Infiniband device
685 * @v priv Private data
686 */
687 static inline __always_inline void
688 ib_set_drvdata ( struct ib_device *ibdev, void *priv ) {
689 ibdev->drv_priv = priv;
690 }
691
692 /**
693 * Get Infiniband device driver-private data
694 *
695 * @v ibdev Infiniband device
696 * @ret priv Private data
697 */
698 static inline __always_inline void *
699 ib_get_drvdata ( struct ib_device *ibdev ) {
700 return ibdev->drv_priv;
701 }
702
703 #endif /* _IPXE_INFINIBAND_H */