1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4  * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #ifndef _IPOIB_H
36 #define _IPOIB_H
37 
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 #include "opt_ofed.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/random.h>
49 #include <sys/rwlock.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/sysctl.h>
53 
54 #include <net/if.h>
55 #include <net/if_arp.h>
56 #include <net/netisr.h>
57 #include <net/route.h>
58 #include <net/if_llc.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
61 #include <net/bpf.h>
62 #include <net/if_llatbl.h>
63 #include <net/vnet.h>
64 
65 #if defined(INET) || defined(INET6)
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/if_ether.h>
69 #include <netinet/ip_var.h>
70 #endif
71 #ifdef INET6
72 #include <netinet6/nd6.h>
73 #endif
74 
75 #include <security/mac/mac_framework.h>
76 
77 #include <linux/list.h>
78 
79 #include <linux/workqueue.h>
80 #include <linux/kref.h>
81 #include <linux/mutex.h>
82 
83 #include <asm/atomic.h>
84 
85 #include <rdma/ib_verbs.h>
86 #include <rdma/ib_pack.h>
87 #include <rdma/ib_sa.h>
88 
89 /* constants */
90 
91 #define	INFINIBAND_ALEN		20	/* Octets in IPoIB HW addr */
92 
93 #ifdef IPOIB_CM
94 #define	CONFIG_INFINIBAND_IPOIB_CM
95 #endif
96 
97 #ifdef IPOIB_DEBUG
98 #define	CONFIG_INFINIBAND_IPOIB_DEBUG
99 #define CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
100 #endif
101 
102 enum ipoib_flush_level {
103 	IPOIB_FLUSH_LIGHT,
104 	IPOIB_FLUSH_NORMAL,
105 	IPOIB_FLUSH_HEAVY
106 };
107 
108 enum {
109 	IPOIB_ENCAP_LEN		  = 4,
110 	IPOIB_HEADER_LEN	  = IPOIB_ENCAP_LEN + INFINIBAND_ALEN,
111 	IPOIB_UD_MAX_MTU	  = 4 * 1024,
112 //	IPOIB_UD_RX_SG		  = (IPOIB_UD_MAX_MTU / MJUMPAGESIZE),
113 	IPOIB_UD_RX_SG		  = 2,
114 	IPOIB_UD_TX_SG		  = (IPOIB_UD_MAX_MTU / MCLBYTES) + 2,
115 	IPOIB_CM_MAX_MTU	  = (64 * 1024),
116 	IPOIB_CM_TX_SG		  = (IPOIB_CM_MAX_MTU / MCLBYTES) + 2,
117 	IPOIB_CM_RX_SG		  = (IPOIB_CM_MAX_MTU / MJUMPAGESIZE),
118 	IPOIB_RX_RING_SIZE	  = 256,
119 	IPOIB_TX_RING_SIZE	  = 128,
120 	IPOIB_MAX_RX_SG		  = MAX(IPOIB_CM_RX_SG, IPOIB_UD_RX_SG),
121 	IPOIB_MAX_TX_SG		  = MAX(IPOIB_CM_TX_SG, IPOIB_UD_TX_SG),
122 	IPOIB_MAX_QUEUE_SIZE	  = 8192,
123 	IPOIB_MIN_QUEUE_SIZE	  = 2,
124 	IPOIB_CM_MAX_CONN_QP	  = 4096,
125 
126 	IPOIB_NUM_WC		  = 4,
127 
128 	IPOIB_MAX_PATH_REC_QUEUE  = 3,
129 	IPOIB_MAX_MCAST_QUEUE	  = 3,
130 
131 	IPOIB_FLAG_OPER_UP	  = 0,
132 	IPOIB_FLAG_INITIALIZED	  = 1,
133 	IPOIB_FLAG_ADMIN_UP	  = 2,
134 	IPOIB_PKEY_ASSIGNED	  = 3,
135 	IPOIB_PKEY_STOP		  = 4,
136 	IPOIB_FLAG_SUBINTERFACE	  = 5,
137 	IPOIB_MCAST_RUN		  = 6,
138 	IPOIB_STOP_REAPER	  = 7,
139 	IPOIB_FLAG_UMCAST	  = 10,
140 	IPOIB_FLAG_CSUM		  = 11,
141 
142 	IPOIB_MAX_BACKOFF_SECONDS = 16,
143 
144 	IPOIB_MCAST_FLAG_FOUND	  = 0,	/* used in set_multicast_list */
145 	IPOIB_MCAST_FLAG_SENDONLY = 1,
146 	IPOIB_MCAST_FLAG_BUSY	  = 2,	/* joining or already joined */
147 	IPOIB_MCAST_FLAG_ATTACHED = 3,
148 
149 	IPOIB_MAX_LRO_DESCRIPTORS = 8,
150 	IPOIB_LRO_MAX_AGGR 	  = 64,
151 
152 	MAX_SEND_CQE		  = 16,
153 	IPOIB_CM_COPYBREAK	  = 256,
154 };
155 
156 #define	IPOIB_OP_RECV   (1ul << 31)
157 #ifdef CONFIG_INFINIBAND_IPOIB_CM
158 #define	IPOIB_OP_CM     (1ul << 30)
159 #else
160 #define	IPOIB_OP_CM     (0)
161 #endif
162 
163 /* structs */
164 
165 struct ipoib_header {
166 	u8  hwaddr[INFINIBAND_ALEN];
167 	__be16	proto;
168 	u16	reserved;
169 };
170 
171 struct ipoib_pseudoheader {
172 	u8  hwaddr[INFINIBAND_ALEN];
173 };
174 
175 /* Used for all multicast joins (broadcast, IPv4 mcast and IPv6 mcast) */
176 struct ipoib_mcast {
177 	struct ib_sa_mcmember_rec mcmember;
178 	struct ib_sa_multicast	 *mc;
179 	struct ipoib_ah		 *ah;
180 
181 	struct rb_node    rb_node;
182 	struct list_head  list;
183 
184 	unsigned long created;
185 	unsigned long backoff;
186 
187 	unsigned long flags;
188 	unsigned char logcount;
189 
190 	struct ifqueue pkt_queue;
191 
192 	struct ipoib_dev_priv *priv;
193 };
194 
195 struct ipoib_cm_rx_buf {
196 	struct mbuf *mb;
197 	u64		mapping[IPOIB_CM_RX_SG];
198 };
199 
200 struct ipoib_cm_tx_buf {
201 	struct mbuf *mb;
202 	u64		mapping[IPOIB_CM_TX_SG];
203 };
204 
205 struct ipoib_rx_buf {
206 	struct mbuf *mb;
207 	u64		mapping[IPOIB_UD_RX_SG];
208 };
209 
210 struct ipoib_tx_buf {
211 	struct mbuf *mb;
212 	u64		mapping[IPOIB_UD_TX_SG];
213 };
214 
215 struct ib_cm_id;
216 
217 struct ipoib_cm_data {
218 	__be32 qpn; /* High byte MUST be ignored on receive */
219 	__be32 mtu;
220 };
221 
222 /*
223  * Quoting 10.3.1 Queue Pair and EE Context States:
224  *
225  * Note, for QPs that are associated with an SRQ, the Consumer should take the
226  * QP through the Error State before invoking a Destroy QP or a Modify QP to the
227  * Reset State.  The Consumer may invoke the Destroy QP without first performing
228  * a Modify QP to the Error State and waiting for the Affiliated Asynchronous
229  * Last WQE Reached Event. However, if the Consumer does not wait for the
230  * Affiliated Asynchronous Last WQE Reached Event, then WQE and Data Segment
231  * leakage may occur. Therefore, it is good programming practice to tear down a
232  * QP that is associated with an SRQ by using the following process:
233  *
234  * - Put the QP in the Error State
235  * - Wait for the Affiliated Asynchronous Last WQE Reached Event;
236  * - either:
237  *       drain the CQ by invoking the Poll CQ verb and either wait for CQ
238  *       to be empty or the number of Poll CQ operations has exceeded
239  *       CQ capacity size;
240  * - or
241  *       post another WR that completes on the same CQ and wait for this
242  *       WR to return as a WC;
243  * - and then invoke a Destroy QP or Reset QP.
244  *
245  * We use the second option and wait for a completion on the
246  * same CQ before destroying QPs attached to our SRQ.
247  */
248 
249 enum ipoib_cm_state {
250 	IPOIB_CM_RX_LIVE,
251 	IPOIB_CM_RX_ERROR, /* Ignored by stale task */
252 	IPOIB_CM_RX_FLUSH  /* Last WQE Reached event observed */
253 };
254 
255 struct ipoib_cm_rx {
256 	struct ib_cm_id	       *id;
257 	struct ib_qp	       *qp;
258 	struct ipoib_cm_rx_buf *rx_ring;
259 	struct list_head	list;
260 	struct ipoib_dev_priv	*priv;
261 	unsigned long		jiffies;
262 	enum ipoib_cm_state	state;
263 	int			recv_count;
264 };
265 
266 struct ipoib_cm_tx {
267 	struct ib_cm_id	    *id;
268 	struct ib_qp	    *qp;
269 	struct list_head     list;
270 	struct ipoib_dev_priv *priv;
271 	struct ipoib_path   *path;
272 	struct ipoib_cm_tx_buf *tx_ring;
273 	unsigned	     tx_head;
274 	unsigned	     tx_tail;
275 	unsigned long	     flags;
276 	u32		     mtu;	/* remote specified mtu, with grh. */
277 };
278 
279 struct ipoib_cm_dev_priv {
280 	struct ib_srq	       *srq;
281 	struct ipoib_cm_rx_buf *srq_ring;
282 	struct ib_cm_id	       *id;
283 	struct list_head	passive_ids;   /* state: LIVE */
284 	struct list_head	rx_error_list; /* state: ERROR */
285 	struct list_head	rx_flush_list; /* state: FLUSH, drain not started */
286 	struct list_head	rx_drain_list; /* state: FLUSH, drain started */
287 	struct list_head	rx_reap_list;  /* state: FLUSH, drain done */
288 	struct work_struct      start_task;
289 	struct work_struct      reap_task;
290 	struct work_struct      mb_task;
291 	struct work_struct      rx_reap_task;
292 	struct delayed_work     stale_task;
293 	struct ifqueue     	mb_queue;
294 	struct list_head	start_list;
295 	struct list_head	reap_list;
296 	struct ib_sge		rx_sge[IPOIB_CM_RX_SG];
297 	struct ib_recv_wr	rx_wr;
298 	int			nonsrq_conn_qp;
299 	int			max_cm_mtu;	/* Actual buf size. */
300 	int			num_frags;
301 };
302 
303 struct ipoib_ethtool_st {
304 	u16     coalesce_usecs;
305 	u16     max_coalesced_frames;
306 };
307 
308 /*
309  * Device private locking: network stack tx_lock protects members used
310  * in TX fast path, lock protects everything else.  lock nests inside
311  * of tx_lock (ie tx_lock must be acquired first if needed).
312  */
313 struct ipoib_dev_priv {
314 	spinlock_t lock;
315 
316 	struct ifnet *dev;
317 
318 	u8 broadcastaddr[INFINIBAND_ALEN];
319 
320 	unsigned long flags;
321 
322 	int gone;
323 
324 	struct mutex vlan_mutex;
325 
326 	struct rb_root  path_tree;
327 	struct list_head path_list;
328 
329 	struct ipoib_mcast *broadcast;
330 	struct list_head multicast_list;
331 	struct rb_root multicast_tree;
332 
333 	struct delayed_work pkey_poll_task;
334 	struct delayed_work mcast_task;
335 	struct work_struct carrier_on_task;
336 	struct work_struct flush_light;
337 	struct work_struct flush_normal;
338 	struct work_struct flush_heavy;
339 	struct work_struct restart_task;
340 	struct delayed_work ah_reap_task;
341 
342 	struct ib_device *ca;
343 	u8		  port;
344 	u16		  pkey;
345 	u16		  pkey_index;
346 	struct ib_pd	 *pd;
347 	struct ib_mr	 *mr;
348 	struct ib_cq	 *recv_cq;
349 	struct ib_cq	 *send_cq;
350 	struct ib_qp	 *qp;
351 	u32		  qkey;
352 
353 	union ib_gid local_gid;
354 	u16	     local_lid;
355 
356 	unsigned int admin_mtu;		/* User selected MTU, no GRH. */
357 	unsigned int mcast_mtu;		/* Minus GRH bytes, from mcast group. */
358 	unsigned int max_ib_mtu;	/* Without header, actual buf size. */
359 
360 	struct ipoib_rx_buf *rx_ring;
361 
362 	struct ipoib_tx_buf *tx_ring;
363 	unsigned	     tx_head;
364 	unsigned	     tx_tail;
365 	struct ib_sge	     tx_sge[IPOIB_MAX_TX_SG];
366 	struct ib_send_wr    tx_wr;
367 	unsigned	     tx_outstanding;
368 	struct ib_wc	     send_wc[MAX_SEND_CQE];
369 
370 	struct ib_recv_wr    rx_wr;
371 	struct ib_sge	     rx_sge[IPOIB_MAX_RX_SG];
372 
373 	struct ib_wc ibwc[IPOIB_NUM_WC];
374 
375 	struct list_head dead_ahs;
376 
377 	struct ib_event_handler event_handler;
378 
379 	struct ifnet *parent;
380 	struct list_head child_intfs;
381 	struct list_head list;
382 
383 #ifdef CONFIG_INFINIBAND_IPOIB_CM
384 	struct ipoib_cm_dev_priv cm;
385 #endif
386 
387 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
388 	struct list_head fs_list;
389 	struct dentry *mcg_dentry;
390 	struct dentry *path_dentry;
391 #endif
392 	int	hca_caps;
393 	struct ipoib_ethtool_st ethtool;
394 	struct timer_list poll_timer;
395 };
396 
397 struct ipoib_ah {
398 	struct ipoib_dev_priv *priv;
399 	struct ib_ah	  *ah;
400 	struct list_head   list;
401 	struct kref	   ref;
402 	unsigned	   last_send;
403 };
404 
405 struct ipoib_path {
406 	struct ipoib_dev_priv *priv;
407 	struct rb_node	      rb_node;
408 	struct list_head      list;
409 #ifdef CONFIG_INFINIBAND_IPOIB_CM
410 	uint8_t		      hwaddr[INFINIBAND_ALEN];
411 	struct ipoib_cm_tx   *cm;
412 #endif
413 	struct ipoib_ah      *ah;
414 	struct ib_sa_path_rec pathrec;
415 	struct ifqueue	      queue;
416 
417 	int		      query_id;
418 	struct ib_sa_query   *query;
419 	struct completion     done;
420 
421 	int  		      valid;
422 };
423 
424 /* UD Only transmits encap len but we want the two sizes to be symmetrical. */
425 #define IPOIB_UD_MTU(ib_mtu)		(ib_mtu - IPOIB_ENCAP_LEN)
426 #define	IPOIB_CM_MTU(ib_mtu)		(ib_mtu - 0x10)
427 
428 #define	IPOIB_IS_MULTICAST(addr)	((addr)[4] == 0xff)
429 
430 extern struct workqueue_struct *ipoib_workqueue;
431 
432 #define IPOIB_MTAP_PROTO(_ifp, _m, _proto)			\
433 do {								\
434 	if (bpf_peers_present((_ifp)->if_bpf)) {		\
435 		M_ASSERTVALID(_m);				\
436 		ipoib_mtap_proto((_ifp), (_m), (_proto));	\
437 	}							\
438 } while (0)
439 
440 /* functions */
441 void ipoib_mtap_proto(struct ifnet *ifp, struct mbuf *mb, uint16_t proto);
442 void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr);
443 void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr);
444 
445 struct ipoib_ah *ipoib_create_ah(struct ipoib_dev_priv *,
446 				 struct ib_pd *pd, struct ib_ah_attr *attr);
447 void ipoib_free_ah(struct kref *kref);
ipoib_put_ah(struct ipoib_ah * ah)448 static inline void ipoib_put_ah(struct ipoib_ah *ah)
449 {
450 	kref_put(&ah->ref, ipoib_free_ah);
451 }
452 
453 int ipoib_open(struct ipoib_dev_priv *priv);
454 int ipoib_add_pkey_attr(struct ipoib_dev_priv *priv);
455 int ipoib_add_umcast_attr(struct ipoib_dev_priv *priv);
456 
457 void ipoib_demux(struct ifnet *ifp, struct mbuf *m, u_short proto);
458 
459 void ipoib_send(struct ipoib_dev_priv *priv, struct mbuf *mb,
460 		struct ipoib_ah *address, u32 qpn);
461 void ipoib_reap_ah(struct work_struct *work);
462 
463 void ipoib_mark_paths_invalid(struct ipoib_dev_priv *priv);
464 void ipoib_flush_paths(struct ipoib_dev_priv *priv);
465 struct ipoib_dev_priv *ipoib_intf_alloc(const char *format);
466 
467 int ipoib_ib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca,
468     int port);
469 void ipoib_ib_dev_flush_light(struct work_struct *work);
470 void ipoib_ib_dev_flush_normal(struct work_struct *work);
471 void ipoib_ib_dev_flush_heavy(struct work_struct *work);
472 void ipoib_pkey_event(struct work_struct *work);
473 void ipoib_ib_dev_cleanup(struct ipoib_dev_priv *priv);
474 
475 int ipoib_ib_dev_open(struct ipoib_dev_priv *priv);
476 int ipoib_ib_dev_up(struct ipoib_dev_priv *priv);
477 int ipoib_ib_dev_down(struct ipoib_dev_priv *priv, int flush);
478 int ipoib_ib_dev_stop(struct ipoib_dev_priv *priv, int flush);
479 
480 int ipoib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port);
481 void ipoib_dev_cleanup(struct ipoib_dev_priv *priv);
482 
483 void ipoib_mcast_join_task(struct work_struct *work);
484 void ipoib_mcast_carrier_on_task(struct work_struct *work);
485 void ipoib_mcast_send(struct ipoib_dev_priv *priv, void *mgid, struct mbuf *mb);
486 
487 void ipoib_mcast_restart_task(struct work_struct *work);
488 void ipoib_mcast_restart(struct ipoib_dev_priv *);
489 int ipoib_mcast_start_thread(struct ipoib_dev_priv *priv);
490 int ipoib_mcast_stop_thread(struct ipoib_dev_priv *priv, int flush);
491 
492 void ipoib_mcast_dev_down(struct ipoib_dev_priv *priv);
493 void ipoib_mcast_dev_flush(struct ipoib_dev_priv *priv);
494 
495 void ipoib_path_free(struct ipoib_dev_priv *priv, struct ipoib_path *path);
496 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
497 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct ipoib_dev_priv *priv);
498 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter);
499 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
500 				  union ib_gid *gid,
501 				  unsigned long *created,
502 				  unsigned int *queuelen,
503 				  unsigned int *complete,
504 				  unsigned int *send_only);
505 
506 struct ipoib_path_iter *ipoib_path_iter_init(struct ipoib_dev_priv *priv);
507 int ipoib_path_iter_next(struct ipoib_path_iter *iter);
508 void ipoib_path_iter_read(struct ipoib_path_iter *iter,
509 			  struct ipoib_path *path);
510 #endif
511 
512 int ipoib_change_mtu(struct ipoib_dev_priv *priv, int new_mtu);
513 
514 int ipoib_mcast_attach(struct ipoib_dev_priv *priv, u16 mlid,
515 		       union ib_gid *mgid, int set_qkey);
516 
517 int ipoib_init_qp(struct ipoib_dev_priv *priv);
518 int ipoib_transport_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca);
519 void ipoib_transport_dev_cleanup(struct ipoib_dev_priv *priv);
520 
521 void ipoib_event(struct ib_event_handler *handler,
522 		 struct ib_event *record);
523 
524 void ipoib_pkey_poll(struct work_struct *work);
525 int ipoib_pkey_dev_delay_open(struct ipoib_dev_priv *priv);
526 void ipoib_drain_cq(struct ipoib_dev_priv *priv);
527 
528 int ipoib_dma_map_tx(struct ib_device *ca, struct ipoib_tx_buf *tx_req, int max);
529 void ipoib_dma_unmap_tx(struct ib_device *ca, struct ipoib_tx_buf *tx_req);
530 int ipoib_poll_tx(struct ipoib_dev_priv *priv);
531 
532 void ipoib_dma_unmap_rx(struct ipoib_dev_priv *priv, struct ipoib_rx_buf *rx_req);
533 void ipoib_dma_mb(struct ipoib_dev_priv *priv, struct mbuf *mb, unsigned int length);
534 struct mbuf *ipoib_alloc_map_mb(struct ipoib_dev_priv *priv, struct ipoib_rx_buf *rx_req, int size);
535 
536 
537 void ipoib_set_ethtool_ops(struct ifnet *dev);
538 int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca);
539 
540 #ifdef CONFIG_INFINIBAND_IPOIB_CM
541 
542 #define IPOIB_FLAGS_RC		0x80
543 #define IPOIB_FLAGS_UC		0x40
544 
545 /* We don't support UC connections at the moment */
546 #define IPOIB_CM_SUPPORTED(ha)   (ha[0] & (IPOIB_FLAGS_RC))
547 
548 extern int ipoib_max_conn_qp;
549 
ipoib_cm_admin_enabled(struct ipoib_dev_priv * priv)550 static inline int ipoib_cm_admin_enabled(struct ipoib_dev_priv *priv)
551 {
552 	return IPOIB_CM_SUPPORTED(IF_LLADDR(priv->dev));
553 }
554 
ipoib_cm_enabled(struct ipoib_dev_priv * priv,uint8_t * hwaddr)555 static inline int ipoib_cm_enabled(struct ipoib_dev_priv *priv, uint8_t *hwaddr)
556 {
557 	return IPOIB_CM_SUPPORTED(hwaddr);
558 }
559 
ipoib_cm_up(struct ipoib_path * path)560 static inline int ipoib_cm_up(struct ipoib_path *path)
561 
562 {
563 	return test_bit(IPOIB_FLAG_OPER_UP, &path->cm->flags);
564 }
565 
ipoib_cm_get(struct ipoib_path * path)566 static inline struct ipoib_cm_tx *ipoib_cm_get(struct ipoib_path *path)
567 {
568 	return path->cm;
569 }
570 
ipoib_cm_set(struct ipoib_path * path,struct ipoib_cm_tx * tx)571 static inline void ipoib_cm_set(struct ipoib_path *path, struct ipoib_cm_tx *tx)
572 {
573 	path->cm = tx;
574 }
575 
ipoib_cm_has_srq(struct ipoib_dev_priv * priv)576 static inline int ipoib_cm_has_srq(struct ipoib_dev_priv *priv)
577 {
578 	return !!priv->cm.srq;
579 }
580 
ipoib_cm_max_mtu(struct ipoib_dev_priv * priv)581 static inline unsigned int ipoib_cm_max_mtu(struct ipoib_dev_priv *priv)
582 {
583 	return priv->cm.max_cm_mtu;
584 }
585 
586 void ipoib_cm_send(struct ipoib_dev_priv *priv, struct mbuf *mb, struct ipoib_cm_tx *tx);
587 int ipoib_cm_dev_open(struct ipoib_dev_priv *priv);
588 void ipoib_cm_dev_stop(struct ipoib_dev_priv *priv);
589 int ipoib_cm_dev_init(struct ipoib_dev_priv *priv);
590 int ipoib_cm_add_mode_attr(struct ipoib_dev_priv *priv);
591 void ipoib_cm_dev_cleanup(struct ipoib_dev_priv *priv);
592 struct ipoib_cm_tx *ipoib_cm_create_tx(struct ipoib_dev_priv *priv,
593     struct ipoib_path *path);
594 void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx);
595 void ipoib_cm_mb_too_long(struct ipoib_dev_priv *priv, struct mbuf *mb,
596 			   unsigned int mtu);
597 void ipoib_cm_handle_rx_wc(struct ipoib_dev_priv *priv, struct ib_wc *wc);
598 void ipoib_cm_handle_tx_wc(struct ipoib_dev_priv *priv, struct ib_wc *wc);
599 #else
600 
601 struct ipoib_cm_tx;
602 
603 #define ipoib_max_conn_qp 0
604 
ipoib_cm_admin_enabled(struct ipoib_dev_priv * priv)605 static inline int ipoib_cm_admin_enabled(struct ipoib_dev_priv *priv)
606 {
607 	return 0;
608 }
ipoib_cm_enabled(struct ipoib_dev_priv * priv,uint8_t * hwaddr)609 static inline int ipoib_cm_enabled(struct ipoib_dev_priv *priv, uint8_t *hwaddr)
610 
611 {
612 	return 0;
613 }
614 
ipoib_cm_up(struct ipoib_path * path)615 static inline int ipoib_cm_up(struct ipoib_path *path)
616 
617 {
618 	return 0;
619 }
620 
ipoib_cm_get(struct ipoib_path * path)621 static inline struct ipoib_cm_tx *ipoib_cm_get(struct ipoib_path *path)
622 {
623 	return NULL;
624 }
625 
ipoib_cm_set(struct ipoib_path * path,struct ipoib_cm_tx * tx)626 static inline void ipoib_cm_set(struct ipoib_path *path, struct ipoib_cm_tx *tx)
627 {
628 }
629 
ipoib_cm_has_srq(struct ipoib_dev_priv * priv)630 static inline int ipoib_cm_has_srq(struct ipoib_dev_priv *priv)
631 {
632 	return 0;
633 }
634 
ipoib_cm_max_mtu(struct ipoib_dev_priv * priv)635 static inline unsigned int ipoib_cm_max_mtu(struct ipoib_dev_priv *priv)
636 {
637 	return 0;
638 }
639 
640 static inline
ipoib_cm_send(struct ipoib_dev_priv * priv,struct mbuf * mb,struct ipoib_cm_tx * tx)641 void ipoib_cm_send(struct ipoib_dev_priv *priv, struct mbuf *mb, struct ipoib_cm_tx *tx)
642 {
643 	return;
644 }
645 
646 static inline
ipoib_cm_dev_open(struct ipoib_dev_priv * priv)647 int ipoib_cm_dev_open(struct ipoib_dev_priv *priv)
648 {
649 	return 0;
650 }
651 
652 static inline
ipoib_cm_dev_stop(struct ipoib_dev_priv * priv)653 void ipoib_cm_dev_stop(struct ipoib_dev_priv *priv)
654 {
655 	return;
656 }
657 
658 static inline
ipoib_cm_dev_init(struct ipoib_dev_priv * priv)659 int ipoib_cm_dev_init(struct ipoib_dev_priv *priv)
660 {
661 	return -ENOSYS;
662 }
663 
664 static inline
ipoib_cm_dev_cleanup(struct ipoib_dev_priv * priv)665 void ipoib_cm_dev_cleanup(struct ipoib_dev_priv *priv)
666 {
667 	return;
668 }
669 
670 static inline
ipoib_cm_create_tx(struct ipoib_dev_priv * priv,struct ipoib_path * path)671 struct ipoib_cm_tx *ipoib_cm_create_tx(struct ipoib_dev_priv *priv, struct ipoib_path *path)
672 {
673 	return NULL;
674 }
675 
676 static inline
ipoib_cm_destroy_tx(struct ipoib_cm_tx * tx)677 void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
678 {
679 	return;
680 }
681 
682 static inline
ipoib_cm_add_mode_attr(struct ipoib_dev_priv * priv)683 int ipoib_cm_add_mode_attr(struct ipoib_dev_priv *priv)
684 {
685 	return 0;
686 }
687 
ipoib_cm_mb_too_long(struct ipoib_dev_priv * priv,struct mbuf * mb,unsigned int mtu)688 static inline void ipoib_cm_mb_too_long(struct ipoib_dev_priv *priv, struct mbuf *mb,
689 					 unsigned int mtu)
690 {
691 	m_freem(mb);
692 }
693 
ipoib_cm_handle_rx_wc(struct ipoib_dev_priv * priv,struct ib_wc * wc)694 static inline void ipoib_cm_handle_rx_wc(struct ipoib_dev_priv *priv, struct ib_wc *wc)
695 {
696 }
697 
ipoib_cm_handle_tx_wc(struct ipoib_dev_priv * priv,struct ib_wc * wc)698 static inline void ipoib_cm_handle_tx_wc(struct ipoib_dev_priv *priv, struct ib_wc *wc)
699 {
700 }
701 #endif
702 
703 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
704 void ipoib_create_debug_files(struct ipoib_dev_priv *priv);
705 void ipoib_delete_debug_files(struct ipoib_dev_priv *priv);
706 int ipoib_register_debugfs(void);
707 void ipoib_unregister_debugfs(void);
708 #else
ipoib_create_debug_files(struct ipoib_dev_priv * priv)709 static inline void ipoib_create_debug_files(struct ipoib_dev_priv *priv) { }
ipoib_delete_debug_files(struct ipoib_dev_priv * priv)710 static inline void ipoib_delete_debug_files(struct ipoib_dev_priv *priv) { }
ipoib_register_debugfs(void)711 static inline int ipoib_register_debugfs(void) { return 0; }
ipoib_unregister_debugfs(void)712 static inline void ipoib_unregister_debugfs(void) { }
713 #endif
714 
715 #define ipoib_printk(level, priv, format, arg...)	\
716 	printk(level "%s: " format, if_name(((struct ipoib_dev_priv *) priv)->dev), ## arg)
717 #define ipoib_warn(priv, format, arg...)		\
718 	ipoib_printk(KERN_WARNING, priv, format , ## arg)
719 
720 extern int ipoib_sendq_size;
721 extern int ipoib_recvq_size;
722 
723 extern struct ib_sa_client ipoib_sa_client;
724 
725 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
726 extern int ipoib_debug_level;
727 
728 #define ipoib_dbg(priv, format, arg...)			\
729 	do {						\
730 		if (ipoib_debug_level > 0)			\
731 			ipoib_printk(KERN_DEBUG, priv, format , ## arg); \
732 	} while (0)
733 #define ipoib_dbg_mcast(priv, format, arg...)		\
734 	do {						\
735 		if (mcast_debug_level > 0)		\
736 			ipoib_printk(KERN_DEBUG, priv, format , ## arg); \
737 	} while (0)
738 #else /* CONFIG_INFINIBAND_IPOIB_DEBUG */
739 #define ipoib_dbg(priv, format, arg...)			\
740 	do { (void) (priv); } while (0)
741 #define ipoib_dbg_mcast(priv, format, arg...)		\
742 	do { (void) (priv); } while (0)
743 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
744 
745 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
746 #define ipoib_dbg_data(priv, format, arg...)		\
747 	do {						\
748 		if (data_debug_level > 0)		\
749 			ipoib_printk(KERN_DEBUG, priv, format , ## arg); \
750 	} while (0)
751 #else /* CONFIG_INFINIBAND_IPOIB_DEBUG_DATA */
752 #define ipoib_dbg_data(priv, format, arg...)		\
753 	do { (void) (priv); } while (0)
754 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG_DATA */
755 
756 #define IPOIB_QPN(ha) (be32_to_cpup((__be32 *) ha) & 0xffffff)
757 
758 #endif /* _IPOIB_H */
759