ipoib_main.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /*
  2. * Copyright (c) 2004 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. * $Id: ipoib_main.c 1377 2004-12-23 19:57:12Z roland $
  35. */
  36. #include "ipoib.h"
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/slab.h>
  40. #include <linux/vmalloc.h>
  41. #include <linux/if_arp.h> /* For ARPHRD_xxx */
  42. #include <linux/ip.h>
  43. #include <linux/in.h>
  44. MODULE_AUTHOR("Roland Dreier");
  45. MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
  46. MODULE_LICENSE("Dual BSD/GPL");
  47. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  48. int ipoib_debug_level;
  49. module_param_named(debug_level, ipoib_debug_level, int, 0644);
  50. MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
  51. #endif
  52. static const u8 ipv4_bcast_addr[] = {
  53. 0x00, 0xff, 0xff, 0xff,
  54. 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
  55. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
  56. };
  57. struct workqueue_struct *ipoib_workqueue;
  58. static void ipoib_add_one(struct ib_device *device);
  59. static void ipoib_remove_one(struct ib_device *device);
  60. static struct ib_client ipoib_client = {
  61. .name = "ipoib",
  62. .add = ipoib_add_one,
  63. .remove = ipoib_remove_one
  64. };
  65. int ipoib_open(struct net_device *dev)
  66. {
  67. struct ipoib_dev_priv *priv = netdev_priv(dev);
  68. ipoib_dbg(priv, "bringing up interface\n");
  69. set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  70. if (ipoib_pkey_dev_delay_open(dev))
  71. return 0;
  72. if (ipoib_ib_dev_open(dev))
  73. return -EINVAL;
  74. if (ipoib_ib_dev_up(dev))
  75. return -EINVAL;
  76. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  77. struct ipoib_dev_priv *cpriv;
  78. /* Bring up any child interfaces too */
  79. down(&priv->vlan_mutex);
  80. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  81. int flags;
  82. flags = cpriv->dev->flags;
  83. if (flags & IFF_UP)
  84. continue;
  85. dev_change_flags(cpriv->dev, flags | IFF_UP);
  86. }
  87. up(&priv->vlan_mutex);
  88. }
  89. netif_start_queue(dev);
  90. return 0;
  91. }
  92. static int ipoib_stop(struct net_device *dev)
  93. {
  94. struct ipoib_dev_priv *priv = netdev_priv(dev);
  95. ipoib_dbg(priv, "stopping interface\n");
  96. clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  97. netif_stop_queue(dev);
  98. ipoib_ib_dev_down(dev);
  99. ipoib_ib_dev_stop(dev);
  100. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  101. struct ipoib_dev_priv *cpriv;
  102. /* Bring down any child interfaces too */
  103. down(&priv->vlan_mutex);
  104. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  105. int flags;
  106. flags = cpriv->dev->flags;
  107. if (!(flags & IFF_UP))
  108. continue;
  109. dev_change_flags(cpriv->dev, flags & ~IFF_UP);
  110. }
  111. up(&priv->vlan_mutex);
  112. }
  113. return 0;
  114. }
  115. static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
  116. {
  117. struct ipoib_dev_priv *priv = netdev_priv(dev);
  118. if (new_mtu > IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN)
  119. return -EINVAL;
  120. priv->admin_mtu = new_mtu;
  121. dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
  122. return 0;
  123. }
  124. static struct ipoib_path *__path_find(struct net_device *dev,
  125. union ib_gid *gid)
  126. {
  127. struct ipoib_dev_priv *priv = netdev_priv(dev);
  128. struct rb_node *n = priv->path_tree.rb_node;
  129. struct ipoib_path *path;
  130. int ret;
  131. while (n) {
  132. path = rb_entry(n, struct ipoib_path, rb_node);
  133. ret = memcmp(gid->raw, path->pathrec.dgid.raw,
  134. sizeof (union ib_gid));
  135. if (ret < 0)
  136. n = n->rb_left;
  137. else if (ret > 0)
  138. n = n->rb_right;
  139. else
  140. return path;
  141. }
  142. return NULL;
  143. }
  144. static int __path_add(struct net_device *dev, struct ipoib_path *path)
  145. {
  146. struct ipoib_dev_priv *priv = netdev_priv(dev);
  147. struct rb_node **n = &priv->path_tree.rb_node;
  148. struct rb_node *pn = NULL;
  149. struct ipoib_path *tpath;
  150. int ret;
  151. while (*n) {
  152. pn = *n;
  153. tpath = rb_entry(pn, struct ipoib_path, rb_node);
  154. ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
  155. sizeof (union ib_gid));
  156. if (ret < 0)
  157. n = &pn->rb_left;
  158. else if (ret > 0)
  159. n = &pn->rb_right;
  160. else
  161. return -EEXIST;
  162. }
  163. rb_link_node(&path->rb_node, pn, n);
  164. rb_insert_color(&path->rb_node, &priv->path_tree);
  165. list_add_tail(&path->list, &priv->path_list);
  166. return 0;
  167. }
  168. static void path_free(struct net_device *dev, struct ipoib_path *path)
  169. {
  170. struct ipoib_dev_priv *priv = netdev_priv(dev);
  171. struct ipoib_neigh *neigh, *tn;
  172. struct sk_buff *skb;
  173. unsigned long flags;
  174. while ((skb = __skb_dequeue(&path->queue)))
  175. dev_kfree_skb_irq(skb);
  176. spin_lock_irqsave(&priv->lock, flags);
  177. list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
  178. /*
  179. * It's safe to call ipoib_put_ah() inside priv->lock
  180. * here, because we know that path->ah will always
  181. * hold one more reference, so ipoib_put_ah() will
  182. * never do more than decrement the ref count.
  183. */
  184. if (neigh->ah)
  185. ipoib_put_ah(neigh->ah);
  186. *to_ipoib_neigh(neigh->neighbour) = NULL;
  187. neigh->neighbour->ops->destructor = NULL;
  188. kfree(neigh);
  189. }
  190. spin_unlock_irqrestore(&priv->lock, flags);
  191. if (path->ah)
  192. ipoib_put_ah(path->ah);
  193. kfree(path);
  194. }
  195. void ipoib_flush_paths(struct net_device *dev)
  196. {
  197. struct ipoib_dev_priv *priv = netdev_priv(dev);
  198. struct ipoib_path *path, *tp;
  199. LIST_HEAD(remove_list);
  200. unsigned long flags;
  201. spin_lock_irqsave(&priv->lock, flags);
  202. list_splice(&priv->path_list, &remove_list);
  203. INIT_LIST_HEAD(&priv->path_list);
  204. list_for_each_entry(path, &remove_list, list)
  205. rb_erase(&path->rb_node, &priv->path_tree);
  206. spin_unlock_irqrestore(&priv->lock, flags);
  207. list_for_each_entry_safe(path, tp, &remove_list, list) {
  208. if (path->query)
  209. ib_sa_cancel_query(path->query_id, path->query);
  210. wait_for_completion(&path->done);
  211. path_free(dev, path);
  212. }
  213. }
  214. static void path_rec_completion(int status,
  215. struct ib_sa_path_rec *pathrec,
  216. void *path_ptr)
  217. {
  218. struct ipoib_path *path = path_ptr;
  219. struct net_device *dev = path->dev;
  220. struct ipoib_dev_priv *priv = netdev_priv(dev);
  221. struct ipoib_ah *ah = NULL;
  222. struct ipoib_neigh *neigh;
  223. struct sk_buff_head skqueue;
  224. struct sk_buff *skb;
  225. unsigned long flags;
  226. if (pathrec)
  227. ipoib_dbg(priv, "PathRec LID 0x%04x for GID " IPOIB_GID_FMT "\n",
  228. be16_to_cpu(pathrec->dlid), IPOIB_GID_ARG(pathrec->dgid));
  229. else
  230. ipoib_dbg(priv, "PathRec status %d for GID " IPOIB_GID_FMT "\n",
  231. status, IPOIB_GID_ARG(path->pathrec.dgid));
  232. skb_queue_head_init(&skqueue);
  233. if (!status) {
  234. struct ib_ah_attr av = {
  235. .dlid = be16_to_cpu(pathrec->dlid),
  236. .sl = pathrec->sl,
  237. .port_num = priv->port
  238. };
  239. int path_rate = ib_sa_rate_enum_to_int(pathrec->rate);
  240. if (path_rate > 0 && priv->local_rate > path_rate)
  241. av.static_rate = (priv->local_rate - 1) / path_rate;
  242. ipoib_dbg(priv, "static_rate %d for local port %dX, path %dX\n",
  243. av.static_rate, priv->local_rate,
  244. ib_sa_rate_enum_to_int(pathrec->rate));
  245. ah = ipoib_create_ah(dev, priv->pd, &av);
  246. }
  247. spin_lock_irqsave(&priv->lock, flags);
  248. path->ah = ah;
  249. if (ah) {
  250. path->pathrec = *pathrec;
  251. ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
  252. ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
  253. while ((skb = __skb_dequeue(&path->queue)))
  254. __skb_queue_tail(&skqueue, skb);
  255. list_for_each_entry(neigh, &path->neigh_list, list) {
  256. kref_get(&path->ah->ref);
  257. neigh->ah = path->ah;
  258. while ((skb = __skb_dequeue(&neigh->queue)))
  259. __skb_queue_tail(&skqueue, skb);
  260. }
  261. } else
  262. path->query = NULL;
  263. complete(&path->done);
  264. spin_unlock_irqrestore(&priv->lock, flags);
  265. while ((skb = __skb_dequeue(&skqueue))) {
  266. skb->dev = dev;
  267. if (dev_queue_xmit(skb))
  268. ipoib_warn(priv, "dev_queue_xmit failed "
  269. "to requeue packet\n");
  270. }
  271. }
  272. static struct ipoib_path *path_rec_create(struct net_device *dev,
  273. union ib_gid *gid)
  274. {
  275. struct ipoib_dev_priv *priv = netdev_priv(dev);
  276. struct ipoib_path *path;
  277. path = kmalloc(sizeof *path, GFP_ATOMIC);
  278. if (!path)
  279. return NULL;
  280. path->dev = dev;
  281. path->pathrec.dlid = 0;
  282. path->ah = NULL;
  283. skb_queue_head_init(&path->queue);
  284. INIT_LIST_HEAD(&path->neigh_list);
  285. path->query = NULL;
  286. init_completion(&path->done);
  287. memcpy(path->pathrec.dgid.raw, gid->raw, sizeof (union ib_gid));
  288. path->pathrec.sgid = priv->local_gid;
  289. path->pathrec.pkey = cpu_to_be16(priv->pkey);
  290. path->pathrec.numb_path = 1;
  291. return path;
  292. }
  293. static int path_rec_start(struct net_device *dev,
  294. struct ipoib_path *path)
  295. {
  296. struct ipoib_dev_priv *priv = netdev_priv(dev);
  297. ipoib_dbg(priv, "Start path record lookup for " IPOIB_GID_FMT "\n",
  298. IPOIB_GID_ARG(path->pathrec.dgid));
  299. path->query_id =
  300. ib_sa_path_rec_get(priv->ca, priv->port,
  301. &path->pathrec,
  302. IB_SA_PATH_REC_DGID |
  303. IB_SA_PATH_REC_SGID |
  304. IB_SA_PATH_REC_NUMB_PATH |
  305. IB_SA_PATH_REC_PKEY,
  306. 1000, GFP_ATOMIC,
  307. path_rec_completion,
  308. path, &path->query);
  309. if (path->query_id < 0) {
  310. ipoib_warn(priv, "ib_sa_path_rec_get failed\n");
  311. path->query = NULL;
  312. return path->query_id;
  313. }
  314. return 0;
  315. }
  316. static void neigh_add_path(struct sk_buff *skb, struct net_device *dev)
  317. {
  318. struct ipoib_dev_priv *priv = netdev_priv(dev);
  319. struct ipoib_path *path;
  320. struct ipoib_neigh *neigh;
  321. neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
  322. if (!neigh) {
  323. ++priv->stats.tx_dropped;
  324. dev_kfree_skb_any(skb);
  325. return;
  326. }
  327. skb_queue_head_init(&neigh->queue);
  328. neigh->neighbour = skb->dst->neighbour;
  329. *to_ipoib_neigh(skb->dst->neighbour) = neigh;
  330. /*
  331. * We can only be called from ipoib_start_xmit, so we're
  332. * inside tx_lock -- no need to save/restore flags.
  333. */
  334. spin_lock(&priv->lock);
  335. path = __path_find(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4));
  336. if (!path) {
  337. path = path_rec_create(dev,
  338. (union ib_gid *) (skb->dst->neighbour->ha + 4));
  339. if (!path)
  340. goto err;
  341. __path_add(dev, path);
  342. }
  343. list_add_tail(&neigh->list, &path->neigh_list);
  344. if (path->pathrec.dlid) {
  345. kref_get(&path->ah->ref);
  346. neigh->ah = path->ah;
  347. ipoib_send(dev, skb, path->ah,
  348. be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
  349. } else {
  350. neigh->ah = NULL;
  351. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  352. __skb_queue_tail(&neigh->queue, skb);
  353. } else {
  354. ++priv->stats.tx_dropped;
  355. dev_kfree_skb_any(skb);
  356. }
  357. if (!path->query && path_rec_start(dev, path))
  358. goto err;
  359. }
  360. spin_unlock(&priv->lock);
  361. return;
  362. err:
  363. *to_ipoib_neigh(skb->dst->neighbour) = NULL;
  364. list_del(&neigh->list);
  365. neigh->neighbour->ops->destructor = NULL;
  366. kfree(neigh);
  367. ++priv->stats.tx_dropped;
  368. dev_kfree_skb_any(skb);
  369. spin_unlock(&priv->lock);
  370. }
  371. static void path_lookup(struct sk_buff *skb, struct net_device *dev)
  372. {
  373. struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
  374. /* Look up path record for unicasts */
  375. if (skb->dst->neighbour->ha[4] != 0xff) {
  376. neigh_add_path(skb, dev);
  377. return;
  378. }
  379. /* Add in the P_Key for multicasts */
  380. skb->dst->neighbour->ha[8] = (priv->pkey >> 8) & 0xff;
  381. skb->dst->neighbour->ha[9] = priv->pkey & 0xff;
  382. ipoib_mcast_send(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4), skb);
  383. }
  384. static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
  385. struct ipoib_pseudoheader *phdr)
  386. {
  387. struct ipoib_dev_priv *priv = netdev_priv(dev);
  388. struct ipoib_path *path;
  389. /*
  390. * We can only be called from ipoib_start_xmit, so we're
  391. * inside tx_lock -- no need to save/restore flags.
  392. */
  393. spin_lock(&priv->lock);
  394. path = __path_find(dev, (union ib_gid *) (phdr->hwaddr + 4));
  395. if (!path) {
  396. path = path_rec_create(dev,
  397. (union ib_gid *) (phdr->hwaddr + 4));
  398. if (path) {
  399. /* put pseudoheader back on for next time */
  400. skb_push(skb, sizeof *phdr);
  401. __skb_queue_tail(&path->queue, skb);
  402. if (path_rec_start(dev, path)) {
  403. spin_unlock(&priv->lock);
  404. path_free(dev, path);
  405. return;
  406. } else
  407. __path_add(dev, path);
  408. } else {
  409. ++priv->stats.tx_dropped;
  410. dev_kfree_skb_any(skb);
  411. }
  412. spin_unlock(&priv->lock);
  413. return;
  414. }
  415. if (path->pathrec.dlid) {
  416. ipoib_dbg(priv, "Send unicast ARP to %04x\n",
  417. be16_to_cpu(path->pathrec.dlid));
  418. ipoib_send(dev, skb, path->ah,
  419. be32_to_cpup((__be32 *) phdr->hwaddr));
  420. } else if ((path->query || !path_rec_start(dev, path)) &&
  421. skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  422. /* put pseudoheader back on for next time */
  423. skb_push(skb, sizeof *phdr);
  424. __skb_queue_tail(&path->queue, skb);
  425. } else {
  426. ++priv->stats.tx_dropped;
  427. dev_kfree_skb_any(skb);
  428. }
  429. spin_unlock(&priv->lock);
  430. }
  431. static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
  432. {
  433. struct ipoib_dev_priv *priv = netdev_priv(dev);
  434. struct ipoib_neigh *neigh;
  435. unsigned long flags;
  436. local_irq_save(flags);
  437. if (!spin_trylock(&priv->tx_lock)) {
  438. local_irq_restore(flags);
  439. return NETDEV_TX_LOCKED;
  440. }
  441. /*
  442. * Check if our queue is stopped. Since we have the LLTX bit
  443. * set, we can't rely on netif_stop_queue() preventing our
  444. * xmit function from being called with a full queue.
  445. */
  446. if (unlikely(netif_queue_stopped(dev))) {
  447. spin_unlock_irqrestore(&priv->tx_lock, flags);
  448. return NETDEV_TX_BUSY;
  449. }
  450. if (skb->dst && skb->dst->neighbour) {
  451. if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) {
  452. path_lookup(skb, dev);
  453. goto out;
  454. }
  455. neigh = *to_ipoib_neigh(skb->dst->neighbour);
  456. if (likely(neigh->ah)) {
  457. ipoib_send(dev, skb, neigh->ah,
  458. be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
  459. goto out;
  460. }
  461. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  462. spin_lock(&priv->lock);
  463. __skb_queue_tail(&neigh->queue, skb);
  464. spin_unlock(&priv->lock);
  465. } else {
  466. ++priv->stats.tx_dropped;
  467. dev_kfree_skb_any(skb);
  468. }
  469. } else {
  470. struct ipoib_pseudoheader *phdr =
  471. (struct ipoib_pseudoheader *) skb->data;
  472. skb_pull(skb, sizeof *phdr);
  473. if (phdr->hwaddr[4] == 0xff) {
  474. /* Add in the P_Key for multicast*/
  475. phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
  476. phdr->hwaddr[9] = priv->pkey & 0xff;
  477. ipoib_mcast_send(dev, (union ib_gid *) (phdr->hwaddr + 4), skb);
  478. } else {
  479. /* unicast GID -- should be ARP or RARP reply */
  480. if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
  481. (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
  482. ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x "
  483. IPOIB_GID_FMT "\n",
  484. skb->dst ? "neigh" : "dst",
  485. be16_to_cpup((__be16 *) skb->data),
  486. be32_to_cpup((__be32 *) phdr->hwaddr),
  487. IPOIB_GID_ARG(*(union ib_gid *) (phdr->hwaddr + 4)));
  488. dev_kfree_skb_any(skb);
  489. ++priv->stats.tx_dropped;
  490. goto out;
  491. }
  492. unicast_arp_send(skb, dev, phdr);
  493. }
  494. }
  495. out:
  496. spin_unlock_irqrestore(&priv->tx_lock, flags);
  497. return NETDEV_TX_OK;
  498. }
  499. static struct net_device_stats *ipoib_get_stats(struct net_device *dev)
  500. {
  501. struct ipoib_dev_priv *priv = netdev_priv(dev);
  502. return &priv->stats;
  503. }
  504. static void ipoib_timeout(struct net_device *dev)
  505. {
  506. struct ipoib_dev_priv *priv = netdev_priv(dev);
  507. ipoib_warn(priv, "transmit timeout: latency %ld\n",
  508. jiffies - dev->trans_start);
  509. /* XXX reset QP, etc. */
  510. }
  511. static int ipoib_hard_header(struct sk_buff *skb,
  512. struct net_device *dev,
  513. unsigned short type,
  514. void *daddr, void *saddr, unsigned len)
  515. {
  516. struct ipoib_header *header;
  517. header = (struct ipoib_header *) skb_push(skb, sizeof *header);
  518. header->proto = htons(type);
  519. header->reserved = 0;
  520. /*
  521. * If we don't have a neighbour structure, stuff the
  522. * destination address onto the front of the skb so we can
  523. * figure out where to send the packet later.
  524. */
  525. if (!skb->dst || !skb->dst->neighbour) {
  526. struct ipoib_pseudoheader *phdr =
  527. (struct ipoib_pseudoheader *) skb_push(skb, sizeof *phdr);
  528. memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
  529. }
  530. return 0;
  531. }
  532. static void ipoib_set_mcast_list(struct net_device *dev)
  533. {
  534. struct ipoib_dev_priv *priv = netdev_priv(dev);
  535. queue_work(ipoib_workqueue, &priv->restart_task);
  536. }
  537. static void ipoib_neigh_destructor(struct neighbour *n)
  538. {
  539. struct ipoib_neigh *neigh;
  540. struct ipoib_dev_priv *priv = netdev_priv(n->dev);
  541. unsigned long flags;
  542. struct ipoib_ah *ah = NULL;
  543. ipoib_dbg(priv,
  544. "neigh_destructor for %06x " IPOIB_GID_FMT "\n",
  545. be32_to_cpup((__be32 *) n->ha),
  546. IPOIB_GID_ARG(*((union ib_gid *) (n->ha + 4))));
  547. spin_lock_irqsave(&priv->lock, flags);
  548. neigh = *to_ipoib_neigh(n);
  549. if (neigh) {
  550. if (neigh->ah)
  551. ah = neigh->ah;
  552. list_del(&neigh->list);
  553. *to_ipoib_neigh(n) = NULL;
  554. kfree(neigh);
  555. }
  556. spin_unlock_irqrestore(&priv->lock, flags);
  557. if (ah)
  558. ipoib_put_ah(ah);
  559. }
  560. static int ipoib_neigh_setup(struct neighbour *neigh)
  561. {
  562. /*
  563. * Is this kosher? I can't find anybody in the kernel that
  564. * sets neigh->destructor, so we should be able to set it here
  565. * without trouble.
  566. */
  567. neigh->ops->destructor = ipoib_neigh_destructor;
  568. return 0;
  569. }
  570. static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
  571. {
  572. parms->neigh_setup = ipoib_neigh_setup;
  573. return 0;
  574. }
  575. int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  576. {
  577. struct ipoib_dev_priv *priv = netdev_priv(dev);
  578. /* Allocate RX/TX "rings" to hold queued skbs */
  579. priv->rx_ring = kmalloc(IPOIB_RX_RING_SIZE * sizeof (struct ipoib_buf),
  580. GFP_KERNEL);
  581. if (!priv->rx_ring) {
  582. printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
  583. ca->name, IPOIB_RX_RING_SIZE);
  584. goto out;
  585. }
  586. memset(priv->rx_ring, 0,
  587. IPOIB_RX_RING_SIZE * sizeof (struct ipoib_buf));
  588. priv->tx_ring = kmalloc(IPOIB_TX_RING_SIZE * sizeof (struct ipoib_buf),
  589. GFP_KERNEL);
  590. if (!priv->tx_ring) {
  591. printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
  592. ca->name, IPOIB_TX_RING_SIZE);
  593. goto out_rx_ring_cleanup;
  594. }
  595. memset(priv->tx_ring, 0,
  596. IPOIB_TX_RING_SIZE * sizeof (struct ipoib_buf));
  597. /* priv->tx_head & tx_tail are already 0 */
  598. if (ipoib_ib_dev_init(dev, ca, port))
  599. goto out_tx_ring_cleanup;
  600. return 0;
  601. out_tx_ring_cleanup:
  602. kfree(priv->tx_ring);
  603. out_rx_ring_cleanup:
  604. kfree(priv->rx_ring);
  605. out:
  606. return -ENOMEM;
  607. }
  608. void ipoib_dev_cleanup(struct net_device *dev)
  609. {
  610. struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
  611. ipoib_delete_debug_file(dev);
  612. /* Delete any child interfaces first */
  613. list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
  614. unregister_netdev(cpriv->dev);
  615. ipoib_dev_cleanup(cpriv->dev);
  616. free_netdev(cpriv->dev);
  617. }
  618. ipoib_ib_dev_cleanup(dev);
  619. kfree(priv->rx_ring);
  620. kfree(priv->tx_ring);
  621. priv->rx_ring = NULL;
  622. priv->tx_ring = NULL;
  623. }
  624. static void ipoib_setup(struct net_device *dev)
  625. {
  626. struct ipoib_dev_priv *priv = netdev_priv(dev);
  627. dev->open = ipoib_open;
  628. dev->stop = ipoib_stop;
  629. dev->change_mtu = ipoib_change_mtu;
  630. dev->hard_start_xmit = ipoib_start_xmit;
  631. dev->get_stats = ipoib_get_stats;
  632. dev->tx_timeout = ipoib_timeout;
  633. dev->hard_header = ipoib_hard_header;
  634. dev->set_multicast_list = ipoib_set_mcast_list;
  635. dev->neigh_setup = ipoib_neigh_setup_dev;
  636. dev->watchdog_timeo = HZ;
  637. dev->rebuild_header = NULL;
  638. dev->set_mac_address = NULL;
  639. dev->header_cache_update = NULL;
  640. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  641. /*
  642. * We add in INFINIBAND_ALEN to allow for the destination
  643. * address "pseudoheader" for skbs without neighbour struct.
  644. */
  645. dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
  646. dev->addr_len = INFINIBAND_ALEN;
  647. dev->type = ARPHRD_INFINIBAND;
  648. dev->tx_queue_len = IPOIB_TX_RING_SIZE * 2;
  649. dev->features = NETIF_F_VLAN_CHALLENGED | NETIF_F_LLTX;
  650. /* MTU will be reset when mcast join happens */
  651. dev->mtu = IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN;
  652. priv->mcast_mtu = priv->admin_mtu = dev->mtu;
  653. memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
  654. netif_carrier_off(dev);
  655. SET_MODULE_OWNER(dev);
  656. priv->dev = dev;
  657. spin_lock_init(&priv->lock);
  658. spin_lock_init(&priv->tx_lock);
  659. init_MUTEX(&priv->mcast_mutex);
  660. init_MUTEX(&priv->vlan_mutex);
  661. INIT_LIST_HEAD(&priv->path_list);
  662. INIT_LIST_HEAD(&priv->child_intfs);
  663. INIT_LIST_HEAD(&priv->dead_ahs);
  664. INIT_LIST_HEAD(&priv->multicast_list);
  665. INIT_WORK(&priv->pkey_task, ipoib_pkey_poll, priv->dev);
  666. INIT_WORK(&priv->mcast_task, ipoib_mcast_join_task, priv->dev);
  667. INIT_WORK(&priv->flush_task, ipoib_ib_dev_flush, priv->dev);
  668. INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task, priv->dev);
  669. INIT_WORK(&priv->ah_reap_task, ipoib_reap_ah, priv->dev);
  670. }
  671. struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
  672. {
  673. struct net_device *dev;
  674. dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
  675. ipoib_setup);
  676. if (!dev)
  677. return NULL;
  678. return netdev_priv(dev);
  679. }
  680. static ssize_t show_pkey(struct class_device *cdev, char *buf)
  681. {
  682. struct ipoib_dev_priv *priv =
  683. netdev_priv(container_of(cdev, struct net_device, class_dev));
  684. return sprintf(buf, "0x%04x\n", priv->pkey);
  685. }
  686. static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
  687. static ssize_t create_child(struct class_device *cdev,
  688. const char *buf, size_t count)
  689. {
  690. int pkey;
  691. int ret;
  692. if (sscanf(buf, "%i", &pkey) != 1)
  693. return -EINVAL;
  694. if (pkey < 0 || pkey > 0xffff)
  695. return -EINVAL;
  696. /*
  697. * Set the full membership bit, so that we join the right
  698. * broadcast group, etc.
  699. */
  700. pkey |= 0x8000;
  701. ret = ipoib_vlan_add(container_of(cdev, struct net_device, class_dev),
  702. pkey);
  703. return ret ? ret : count;
  704. }
  705. static CLASS_DEVICE_ATTR(create_child, S_IWUGO, NULL, create_child);
  706. static ssize_t delete_child(struct class_device *cdev,
  707. const char *buf, size_t count)
  708. {
  709. int pkey;
  710. int ret;
  711. if (sscanf(buf, "%i", &pkey) != 1)
  712. return -EINVAL;
  713. if (pkey < 0 || pkey > 0xffff)
  714. return -EINVAL;
  715. ret = ipoib_vlan_delete(container_of(cdev, struct net_device, class_dev),
  716. pkey);
  717. return ret ? ret : count;
  718. }
  719. static CLASS_DEVICE_ATTR(delete_child, S_IWUGO, NULL, delete_child);
  720. int ipoib_add_pkey_attr(struct net_device *dev)
  721. {
  722. return class_device_create_file(&dev->class_dev,
  723. &class_device_attr_pkey);
  724. }
  725. static struct net_device *ipoib_add_port(const char *format,
  726. struct ib_device *hca, u8 port)
  727. {
  728. struct ipoib_dev_priv *priv;
  729. int result = -ENOMEM;
  730. priv = ipoib_intf_alloc(format);
  731. if (!priv)
  732. goto alloc_mem_failed;
  733. SET_NETDEV_DEV(priv->dev, hca->dma_device);
  734. result = ib_query_pkey(hca, port, 0, &priv->pkey);
  735. if (result) {
  736. printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
  737. hca->name, port, result);
  738. goto alloc_mem_failed;
  739. }
  740. /*
  741. * Set the full membership bit, so that we join the right
  742. * broadcast group, etc.
  743. */
  744. priv->pkey |= 0x8000;
  745. priv->dev->broadcast[8] = priv->pkey >> 8;
  746. priv->dev->broadcast[9] = priv->pkey & 0xff;
  747. result = ib_query_gid(hca, port, 0, &priv->local_gid);
  748. if (result) {
  749. printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
  750. hca->name, port, result);
  751. goto alloc_mem_failed;
  752. } else
  753. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  754. result = ipoib_dev_init(priv->dev, hca, port);
  755. if (result < 0) {
  756. printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
  757. hca->name, port, result);
  758. goto device_init_failed;
  759. }
  760. INIT_IB_EVENT_HANDLER(&priv->event_handler,
  761. priv->ca, ipoib_event);
  762. result = ib_register_event_handler(&priv->event_handler);
  763. if (result < 0) {
  764. printk(KERN_WARNING "%s: ib_register_event_handler failed for "
  765. "port %d (ret = %d)\n",
  766. hca->name, port, result);
  767. goto event_failed;
  768. }
  769. result = register_netdev(priv->dev);
  770. if (result) {
  771. printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
  772. hca->name, port, result);
  773. goto register_failed;
  774. }
  775. if (ipoib_create_debug_file(priv->dev))
  776. goto debug_failed;
  777. if (ipoib_add_pkey_attr(priv->dev))
  778. goto sysfs_failed;
  779. if (class_device_create_file(&priv->dev->class_dev,
  780. &class_device_attr_create_child))
  781. goto sysfs_failed;
  782. if (class_device_create_file(&priv->dev->class_dev,
  783. &class_device_attr_delete_child))
  784. goto sysfs_failed;
  785. return priv->dev;
  786. sysfs_failed:
  787. ipoib_delete_debug_file(priv->dev);
  788. debug_failed:
  789. unregister_netdev(priv->dev);
  790. register_failed:
  791. ib_unregister_event_handler(&priv->event_handler);
  792. event_failed:
  793. ipoib_dev_cleanup(priv->dev);
  794. device_init_failed:
  795. free_netdev(priv->dev);
  796. alloc_mem_failed:
  797. return ERR_PTR(result);
  798. }
  799. static void ipoib_add_one(struct ib_device *device)
  800. {
  801. struct list_head *dev_list;
  802. struct net_device *dev;
  803. struct ipoib_dev_priv *priv;
  804. int s, e, p;
  805. dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
  806. if (!dev_list)
  807. return;
  808. INIT_LIST_HEAD(dev_list);
  809. if (device->node_type == IB_NODE_SWITCH) {
  810. s = 0;
  811. e = 0;
  812. } else {
  813. s = 1;
  814. e = device->phys_port_cnt;
  815. }
  816. for (p = s; p <= e; ++p) {
  817. dev = ipoib_add_port("ib%d", device, p);
  818. if (!IS_ERR(dev)) {
  819. priv = netdev_priv(dev);
  820. list_add_tail(&priv->list, dev_list);
  821. }
  822. }
  823. ib_set_client_data(device, &ipoib_client, dev_list);
  824. }
  825. static void ipoib_remove_one(struct ib_device *device)
  826. {
  827. struct ipoib_dev_priv *priv, *tmp;
  828. struct list_head *dev_list;
  829. dev_list = ib_get_client_data(device, &ipoib_client);
  830. list_for_each_entry_safe(priv, tmp, dev_list, list) {
  831. ib_unregister_event_handler(&priv->event_handler);
  832. unregister_netdev(priv->dev);
  833. ipoib_dev_cleanup(priv->dev);
  834. free_netdev(priv->dev);
  835. }
  836. kfree(dev_list);
  837. }
  838. static int __init ipoib_init_module(void)
  839. {
  840. int ret;
  841. ret = ipoib_register_debugfs();
  842. if (ret)
  843. return ret;
  844. /*
  845. * We create our own workqueue mainly because we want to be
  846. * able to flush it when devices are being removed. We can't
  847. * use schedule_work()/flush_scheduled_work() because both
  848. * unregister_netdev() and linkwatch_event take the rtnl lock,
  849. * so flush_scheduled_work() can deadlock during device
  850. * removal.
  851. */
  852. ipoib_workqueue = create_singlethread_workqueue("ipoib");
  853. if (!ipoib_workqueue) {
  854. ret = -ENOMEM;
  855. goto err_fs;
  856. }
  857. ret = ib_register_client(&ipoib_client);
  858. if (ret)
  859. goto err_wq;
  860. return 0;
  861. err_wq:
  862. destroy_workqueue(ipoib_workqueue);
  863. err_fs:
  864. ipoib_unregister_debugfs();
  865. return ret;
  866. }
  867. static void __exit ipoib_cleanup_module(void)
  868. {
  869. ib_unregister_client(&ipoib_client);
  870. ipoib_unregister_debugfs();
  871. destroy_workqueue(ipoib_workqueue);
  872. }
  873. module_init(ipoib_init_module);
  874. module_exit(ipoib_cleanup_module);