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.
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:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
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.
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
38 #include <linux/list.h>
39 #include <linux/skbuff.h>
40 #include <linux/netdevice.h>
41 #include <linux/workqueue.h>
42 #include <linux/kref.h>
43 #include <linux/if_infiniband.h>
44 #include <linux/mutex.h>
46 #include <net/neighbour.h>
47 #include <net/sch_generic.h>
49 #include <linux/atomic.h>
51 #include <rdma/ib_verbs.h>
52 #include <rdma/ib_pack.h>
53 #include <rdma/ib_sa.h>
54 #include <linux/sched.h>
58 enum ipoib_flush_level {
67 IPOIB_UD_HEAD_SIZE = IB_GRH_BYTES + IPOIB_ENCAP_LEN,
68 IPOIB_UD_RX_SG = 2, /* max buffer needed for 4K mtu */
70 IPOIB_CM_MTU = 0x10000 - 0x10, /* padding to align header to 16 */
71 IPOIB_CM_BUF_SIZE = IPOIB_CM_MTU + IPOIB_ENCAP_LEN,
72 IPOIB_CM_HEAD_SIZE = IPOIB_CM_BUF_SIZE % PAGE_SIZE,
73 IPOIB_CM_RX_SG = ALIGN(IPOIB_CM_BUF_SIZE, PAGE_SIZE) / PAGE_SIZE,
74 IPOIB_RX_RING_SIZE = 256,
75 IPOIB_TX_RING_SIZE = 128,
76 IPOIB_MAX_QUEUE_SIZE = 8192,
77 IPOIB_MIN_QUEUE_SIZE = 2,
78 IPOIB_CM_MAX_CONN_QP = 4096,
82 IPOIB_MAX_PATH_REC_QUEUE = 3,
83 IPOIB_MAX_MCAST_QUEUE = 3,
85 IPOIB_FLAG_OPER_UP = 0,
86 IPOIB_FLAG_INITIALIZED = 1,
87 IPOIB_FLAG_ADMIN_UP = 2,
88 IPOIB_PKEY_ASSIGNED = 3,
90 IPOIB_FLAG_SUBINTERFACE = 5,
92 IPOIB_STOP_REAPER = 7,
93 IPOIB_FLAG_ADMIN_CM = 9,
94 IPOIB_FLAG_UMCAST = 10,
96 IPOIB_MAX_BACKOFF_SECONDS = 16,
98 IPOIB_MCAST_FLAG_FOUND = 0, /* used in set_multicast_list */
99 IPOIB_MCAST_FLAG_SENDONLY = 1,
100 IPOIB_MCAST_FLAG_BUSY = 2, /* joining or already joined */
101 IPOIB_MCAST_FLAG_ATTACHED = 3,
104 IPOIB_CM_COPYBREAK = 256,
107 #define IPOIB_OP_RECV (1ul << 31)
108 #ifdef CONFIG_INFINIBAND_IPOIB_CM
109 #define IPOIB_OP_CM (1ul << 30)
111 #define IPOIB_OP_CM (0)
116 struct ipoib_header {
122 struct qdisc_skb_cb qdisc_cb;
123 u8 hwaddr[INFINIBAND_ALEN];
126 /* Used for all multicast joins (broadcast, IPv4 mcast and IPv6 mcast) */
128 struct ib_sa_mcmember_rec mcmember;
129 struct ib_sa_multicast *mc;
132 struct rb_node rb_node;
133 struct list_head list;
135 unsigned long created;
136 unsigned long backoff;
139 unsigned char logcount;
141 struct list_head neigh_list;
143 struct sk_buff_head pkt_queue;
145 struct net_device *dev;
148 struct ipoib_rx_buf {
150 u64 mapping[IPOIB_UD_RX_SG];
153 struct ipoib_tx_buf {
155 u64 mapping[MAX_SKB_FRAGS + 1];
158 struct ipoib_cm_tx_buf {
165 struct ipoib_cm_data {
166 __be32 qpn; /* High byte MUST be ignored on receive */
171 * Quoting 10.3.1 Queue Pair and EE Context States:
173 * Note, for QPs that are associated with an SRQ, the Consumer should take the
174 * QP through the Error State before invoking a Destroy QP or a Modify QP to the
175 * Reset State. The Consumer may invoke the Destroy QP without first performing
176 * a Modify QP to the Error State and waiting for the Affiliated Asynchronous
177 * Last WQE Reached Event. However, if the Consumer does not wait for the
178 * Affiliated Asynchronous Last WQE Reached Event, then WQE and Data Segment
179 * leakage may occur. Therefore, it is good programming practice to tear down a
180 * QP that is associated with an SRQ by using the following process:
182 * - Put the QP in the Error State
183 * - Wait for the Affiliated Asynchronous Last WQE Reached Event;
185 * drain the CQ by invoking the Poll CQ verb and either wait for CQ
186 * to be empty or the number of Poll CQ operations has exceeded
189 * post another WR that completes on the same CQ and wait for this
190 * WR to return as a WC;
191 * - and then invoke a Destroy QP or Reset QP.
193 * We use the second option and wait for a completion on the
194 * same CQ before destroying QPs attached to our SRQ.
197 enum ipoib_cm_state {
199 IPOIB_CM_RX_ERROR, /* Ignored by stale task */
200 IPOIB_CM_RX_FLUSH /* Last WQE Reached event observed */
206 struct ipoib_cm_rx_buf *rx_ring;
207 struct list_head list;
208 struct net_device *dev;
209 unsigned long jiffies;
210 enum ipoib_cm_state state;
217 struct list_head list;
218 struct net_device *dev;
219 struct ipoib_neigh *neigh;
220 struct ipoib_path *path;
221 struct ipoib_cm_tx_buf *tx_ring;
228 struct ipoib_cm_rx_buf {
230 u64 mapping[IPOIB_CM_RX_SG];
233 struct ipoib_cm_dev_priv {
235 struct ipoib_cm_rx_buf *srq_ring;
237 struct list_head passive_ids; /* state: LIVE */
238 struct list_head rx_error_list; /* state: ERROR */
239 struct list_head rx_flush_list; /* state: FLUSH, drain not started */
240 struct list_head rx_drain_list; /* state: FLUSH, drain started */
241 struct list_head rx_reap_list; /* state: FLUSH, drain done */
242 struct work_struct start_task;
243 struct work_struct reap_task;
244 struct work_struct skb_task;
245 struct work_struct rx_reap_task;
246 struct delayed_work stale_task;
247 struct sk_buff_head skb_queue;
248 struct list_head start_list;
249 struct list_head reap_list;
250 struct ib_wc ibwc[IPOIB_NUM_WC];
251 struct ib_sge rx_sge[IPOIB_CM_RX_SG];
252 struct ib_recv_wr rx_wr;
258 struct ipoib_ethtool_st {
260 u16 max_coalesced_frames;
264 * Device private locking: network stack tx_lock protects members used
265 * in TX fast path, lock protects everything else. lock nests inside
266 * of tx_lock (ie tx_lock must be acquired first if needed).
268 struct ipoib_dev_priv {
271 struct net_device *dev;
273 struct napi_struct napi;
277 struct mutex vlan_mutex;
279 struct rb_root path_tree;
280 struct list_head path_list;
282 struct ipoib_mcast *broadcast;
283 struct list_head multicast_list;
284 struct rb_root multicast_tree;
286 struct delayed_work pkey_poll_task;
287 struct delayed_work mcast_task;
288 struct work_struct carrier_on_task;
289 struct work_struct flush_light;
290 struct work_struct flush_normal;
291 struct work_struct flush_heavy;
292 struct work_struct restart_task;
293 struct delayed_work ah_reap_task;
295 struct ib_device *ca;
301 struct ib_cq *recv_cq;
302 struct ib_cq *send_cq;
306 union ib_gid local_gid;
309 unsigned int admin_mtu;
310 unsigned int mcast_mtu;
311 unsigned int max_ib_mtu;
313 struct ipoib_rx_buf *rx_ring;
315 struct ipoib_tx_buf *tx_ring;
318 struct ib_sge tx_sge[MAX_SKB_FRAGS + 1];
319 struct ib_send_wr tx_wr;
320 unsigned tx_outstanding;
321 struct ib_wc send_wc[MAX_SEND_CQE];
323 struct ib_recv_wr rx_wr;
324 struct ib_sge rx_sge[IPOIB_UD_RX_SG];
326 struct ib_wc ibwc[IPOIB_NUM_WC];
328 struct list_head dead_ahs;
330 struct ib_event_handler event_handler;
332 struct net_device *parent;
333 struct list_head child_intfs;
334 struct list_head list;
336 #ifdef CONFIG_INFINIBAND_IPOIB_CM
337 struct ipoib_cm_dev_priv cm;
340 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
341 struct list_head fs_list;
342 struct dentry *mcg_dentry;
343 struct dentry *path_dentry;
346 struct ipoib_ethtool_st ethtool;
347 struct timer_list poll_timer;
351 struct net_device *dev;
353 struct list_head list;
359 struct net_device *dev;
360 struct ib_sa_path_rec pathrec;
362 struct sk_buff_head queue;
364 struct list_head neigh_list;
367 struct ib_sa_query *query;
368 struct completion done;
370 struct rb_node rb_node;
371 struct list_head list;
377 #ifdef CONFIG_INFINIBAND_IPOIB_CM
378 struct ipoib_cm_tx *cm;
381 struct sk_buff_head queue;
383 struct neighbour *neighbour;
384 struct net_device *dev;
386 struct list_head list;
389 #define IPOIB_UD_MTU(ib_mtu) (ib_mtu - IPOIB_ENCAP_LEN)
390 #define IPOIB_UD_BUF_SIZE(ib_mtu) (ib_mtu + IB_GRH_BYTES)
392 static inline int ipoib_ud_need_sg(unsigned int ib_mtu)
394 return IPOIB_UD_BUF_SIZE(ib_mtu) > PAGE_SIZE;
398 * We stash a pointer to our private neighbour information after our
399 * hardware address in neigh->ha. The ALIGN() expression here makes
400 * sure that this pointer is stored aligned so that an unaligned
401 * load is not needed to dereference it.
403 static inline struct ipoib_neigh **to_ipoib_neigh(struct neighbour *neigh)
405 return (void*) neigh + ALIGN(offsetof(struct neighbour, ha) +
406 INFINIBAND_ALEN, sizeof(void *));
409 struct ipoib_neigh *ipoib_neigh_alloc(struct neighbour *neigh,
410 struct net_device *dev);
411 void ipoib_neigh_free(struct net_device *dev, struct ipoib_neigh *neigh);
413 extern struct workqueue_struct *ipoib_workqueue;
417 int ipoib_poll(struct napi_struct *napi, int budget);
418 void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr);
419 void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr);
421 struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
422 struct ib_pd *pd, struct ib_ah_attr *attr);
423 void ipoib_free_ah(struct kref *kref);
424 static inline void ipoib_put_ah(struct ipoib_ah *ah)
426 kref_put(&ah->ref, ipoib_free_ah);
429 int ipoib_open(struct net_device *dev);
430 int ipoib_add_pkey_attr(struct net_device *dev);
431 int ipoib_add_umcast_attr(struct net_device *dev);
433 void ipoib_send(struct net_device *dev, struct sk_buff *skb,
434 struct ipoib_ah *address, u32 qpn);
435 void ipoib_reap_ah(struct work_struct *work);
437 struct ipoib_path *__path_find(struct net_device *dev, void *gid);
438 void ipoib_mark_paths_invalid(struct net_device *dev);
439 void ipoib_flush_paths(struct net_device *dev);
440 struct ipoib_dev_priv *ipoib_intf_alloc(const char *format);
442 int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port);
443 void ipoib_ib_dev_flush_light(struct work_struct *work);
444 void ipoib_ib_dev_flush_normal(struct work_struct *work);
445 void ipoib_ib_dev_flush_heavy(struct work_struct *work);
446 void ipoib_pkey_event(struct work_struct *work);
447 void ipoib_ib_dev_cleanup(struct net_device *dev);
449 int ipoib_ib_dev_open(struct net_device *dev);
450 int ipoib_ib_dev_up(struct net_device *dev);
451 int ipoib_ib_dev_down(struct net_device *dev, int flush);
452 int ipoib_ib_dev_stop(struct net_device *dev, int flush);
454 int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port);
455 void ipoib_dev_cleanup(struct net_device *dev);
457 void ipoib_mcast_join_task(struct work_struct *work);
458 void ipoib_mcast_carrier_on_task(struct work_struct *work);
459 void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb);
461 void ipoib_mcast_restart_task(struct work_struct *work);
462 int ipoib_mcast_start_thread(struct net_device *dev);
463 int ipoib_mcast_stop_thread(struct net_device *dev, int flush);
465 void ipoib_mcast_dev_down(struct net_device *dev);
466 void ipoib_mcast_dev_flush(struct net_device *dev);
468 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
469 struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev);
470 int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter);
471 void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
473 unsigned long *created,
474 unsigned int *queuelen,
475 unsigned int *complete,
476 unsigned int *send_only);
478 struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev);
479 int ipoib_path_iter_next(struct ipoib_path_iter *iter);
480 void ipoib_path_iter_read(struct ipoib_path_iter *iter,
481 struct ipoib_path *path);
484 int ipoib_mcast_attach(struct net_device *dev, u16 mlid,
485 union ib_gid *mgid, int set_qkey);
487 int ipoib_init_qp(struct net_device *dev);
488 int ipoib_transport_dev_init(struct net_device *dev, struct ib_device *ca);
489 void ipoib_transport_dev_cleanup(struct net_device *dev);
491 void ipoib_event(struct ib_event_handler *handler,
492 struct ib_event *record);
494 int ipoib_vlan_add(struct net_device *pdev, unsigned short pkey);
495 int ipoib_vlan_delete(struct net_device *pdev, unsigned short pkey);
497 void ipoib_pkey_poll(struct work_struct *work);
498 int ipoib_pkey_dev_delay_open(struct net_device *dev);
499 void ipoib_drain_cq(struct net_device *dev);
501 void ipoib_set_ethtool_ops(struct net_device *dev);
502 int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca);
504 #ifdef CONFIG_INFINIBAND_IPOIB_CM
506 #define IPOIB_FLAGS_RC 0x80
507 #define IPOIB_FLAGS_UC 0x40
509 /* We don't support UC connections at the moment */
510 #define IPOIB_CM_SUPPORTED(ha) (ha[0] & (IPOIB_FLAGS_RC))
512 extern int ipoib_max_conn_qp;
514 static inline int ipoib_cm_admin_enabled(struct net_device *dev)
516 struct ipoib_dev_priv *priv = netdev_priv(dev);
517 return IPOIB_CM_SUPPORTED(dev->dev_addr) &&
518 test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
521 static inline int ipoib_cm_enabled(struct net_device *dev, struct neighbour *n)
523 struct ipoib_dev_priv *priv = netdev_priv(dev);
524 return IPOIB_CM_SUPPORTED(n->ha) &&
525 test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
528 static inline int ipoib_cm_up(struct ipoib_neigh *neigh)
531 return test_bit(IPOIB_FLAG_OPER_UP, &neigh->cm->flags);
534 static inline struct ipoib_cm_tx *ipoib_cm_get(struct ipoib_neigh *neigh)
539 static inline void ipoib_cm_set(struct ipoib_neigh *neigh, struct ipoib_cm_tx *tx)
544 static inline int ipoib_cm_has_srq(struct net_device *dev)
546 struct ipoib_dev_priv *priv = netdev_priv(dev);
547 return !!priv->cm.srq;
550 static inline unsigned int ipoib_cm_max_mtu(struct net_device *dev)
552 struct ipoib_dev_priv *priv = netdev_priv(dev);
553 return priv->cm.max_cm_mtu;
556 void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx);
557 int ipoib_cm_dev_open(struct net_device *dev);
558 void ipoib_cm_dev_stop(struct net_device *dev);
559 int ipoib_cm_dev_init(struct net_device *dev);
560 int ipoib_cm_add_mode_attr(struct net_device *dev);
561 void ipoib_cm_dev_cleanup(struct net_device *dev);
562 struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path,
563 struct ipoib_neigh *neigh);
564 void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx);
565 void ipoib_cm_skb_too_long(struct net_device *dev, struct sk_buff *skb,
567 void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc);
568 void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc);
573 #define ipoib_max_conn_qp 0
575 static inline int ipoib_cm_admin_enabled(struct net_device *dev)
579 static inline int ipoib_cm_enabled(struct net_device *dev, struct neighbour *n)
585 static inline int ipoib_cm_up(struct ipoib_neigh *neigh)
591 static inline struct ipoib_cm_tx *ipoib_cm_get(struct ipoib_neigh *neigh)
596 static inline void ipoib_cm_set(struct ipoib_neigh *neigh, struct ipoib_cm_tx *tx)
600 static inline int ipoib_cm_has_srq(struct net_device *dev)
605 static inline unsigned int ipoib_cm_max_mtu(struct net_device *dev)
611 void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx)
617 int ipoib_cm_dev_open(struct net_device *dev)
623 void ipoib_cm_dev_stop(struct net_device *dev)
629 int ipoib_cm_dev_init(struct net_device *dev)
635 void ipoib_cm_dev_cleanup(struct net_device *dev)
641 struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path,
642 struct ipoib_neigh *neigh)
648 void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
654 int ipoib_cm_add_mode_attr(struct net_device *dev)
659 static inline void ipoib_cm_skb_too_long(struct net_device *dev, struct sk_buff *skb,
662 dev_kfree_skb_any(skb);
665 static inline void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
669 static inline void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
674 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
675 void ipoib_create_debug_files(struct net_device *dev);
676 void ipoib_delete_debug_files(struct net_device *dev);
677 int ipoib_register_debugfs(void);
678 void ipoib_unregister_debugfs(void);
680 static inline void ipoib_create_debug_files(struct net_device *dev) { }
681 static inline void ipoib_delete_debug_files(struct net_device *dev) { }
682 static inline int ipoib_register_debugfs(void) { return 0; }
683 static inline void ipoib_unregister_debugfs(void) { }
686 #define ipoib_printk(level, priv, format, arg...) \
687 printk(level "%s: " format, ((struct ipoib_dev_priv *) priv)->dev->name , ## arg)
688 #define ipoib_warn(priv, format, arg...) \
689 ipoib_printk(KERN_WARNING, priv, format , ## arg)
691 extern int ipoib_sendq_size;
692 extern int ipoib_recvq_size;
694 extern struct ib_sa_client ipoib_sa_client;
696 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
697 extern int ipoib_debug_level;
699 #define ipoib_dbg(priv, format, arg...) \
701 if (ipoib_debug_level > 0) \
702 ipoib_printk(KERN_DEBUG, priv, format , ## arg); \
704 #define ipoib_dbg_mcast(priv, format, arg...) \
706 if (mcast_debug_level > 0) \
707 ipoib_printk(KERN_DEBUG, priv, format , ## arg); \
709 #else /* CONFIG_INFINIBAND_IPOIB_DEBUG */
710 #define ipoib_dbg(priv, format, arg...) \
711 do { (void) (priv); } while (0)
712 #define ipoib_dbg_mcast(priv, format, arg...) \
713 do { (void) (priv); } while (0)
714 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
716 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
717 #define ipoib_dbg_data(priv, format, arg...) \
719 if (data_debug_level > 0) \
720 ipoib_printk(KERN_DEBUG, priv, format , ## arg); \
722 #else /* CONFIG_INFINIBAND_IPOIB_DEBUG_DATA */
723 #define ipoib_dbg_data(priv, format, arg...) \
724 do { (void) (priv); } while (0)
725 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG_DATA */
727 #define IPOIB_QPN(ha) (be32_to_cpup((__be32 *) ha) & 0xffffff)
729 #endif /* _IPOIB_H */