ipoib_main.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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 = kzalloc(sizeof *path, GFP_ATOMIC);
  278. if (!path)
  279. return NULL;
  280. path->dev = dev;
  281. skb_queue_head_init(&path->queue);
  282. INIT_LIST_HEAD(&path->neigh_list);
  283. init_completion(&path->done);
  284. memcpy(path->pathrec.dgid.raw, gid->raw, sizeof (union ib_gid));
  285. path->pathrec.sgid = priv->local_gid;
  286. path->pathrec.pkey = cpu_to_be16(priv->pkey);
  287. path->pathrec.numb_path = 1;
  288. return path;
  289. }
  290. static int path_rec_start(struct net_device *dev,
  291. struct ipoib_path *path)
  292. {
  293. struct ipoib_dev_priv *priv = netdev_priv(dev);
  294. ipoib_dbg(priv, "Start path record lookup for " IPOIB_GID_FMT "\n",
  295. IPOIB_GID_ARG(path->pathrec.dgid));
  296. path->query_id =
  297. ib_sa_path_rec_get(priv->ca, priv->port,
  298. &path->pathrec,
  299. IB_SA_PATH_REC_DGID |
  300. IB_SA_PATH_REC_SGID |
  301. IB_SA_PATH_REC_NUMB_PATH |
  302. IB_SA_PATH_REC_PKEY,
  303. 1000, GFP_ATOMIC,
  304. path_rec_completion,
  305. path, &path->query);
  306. if (path->query_id < 0) {
  307. ipoib_warn(priv, "ib_sa_path_rec_get failed\n");
  308. path->query = NULL;
  309. return path->query_id;
  310. }
  311. return 0;
  312. }
  313. static void neigh_add_path(struct sk_buff *skb, struct net_device *dev)
  314. {
  315. struct ipoib_dev_priv *priv = netdev_priv(dev);
  316. struct ipoib_path *path;
  317. struct ipoib_neigh *neigh;
  318. neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
  319. if (!neigh) {
  320. ++priv->stats.tx_dropped;
  321. dev_kfree_skb_any(skb);
  322. return;
  323. }
  324. skb_queue_head_init(&neigh->queue);
  325. neigh->neighbour = skb->dst->neighbour;
  326. *to_ipoib_neigh(skb->dst->neighbour) = neigh;
  327. /*
  328. * We can only be called from ipoib_start_xmit, so we're
  329. * inside tx_lock -- no need to save/restore flags.
  330. */
  331. spin_lock(&priv->lock);
  332. path = __path_find(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4));
  333. if (!path) {
  334. path = path_rec_create(dev,
  335. (union ib_gid *) (skb->dst->neighbour->ha + 4));
  336. if (!path)
  337. goto err;
  338. __path_add(dev, path);
  339. }
  340. list_add_tail(&neigh->list, &path->neigh_list);
  341. if (path->pathrec.dlid) {
  342. kref_get(&path->ah->ref);
  343. neigh->ah = path->ah;
  344. ipoib_send(dev, skb, path->ah,
  345. be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
  346. } else {
  347. neigh->ah = NULL;
  348. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  349. __skb_queue_tail(&neigh->queue, skb);
  350. } else {
  351. ++priv->stats.tx_dropped;
  352. dev_kfree_skb_any(skb);
  353. }
  354. if (!path->query && path_rec_start(dev, path))
  355. goto err;
  356. }
  357. spin_unlock(&priv->lock);
  358. return;
  359. err:
  360. *to_ipoib_neigh(skb->dst->neighbour) = NULL;
  361. list_del(&neigh->list);
  362. neigh->neighbour->ops->destructor = NULL;
  363. kfree(neigh);
  364. ++priv->stats.tx_dropped;
  365. dev_kfree_skb_any(skb);
  366. spin_unlock(&priv->lock);
  367. }
  368. static void ipoib_path_lookup(struct sk_buff *skb, struct net_device *dev)
  369. {
  370. struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
  371. /* Look up path record for unicasts */
  372. if (skb->dst->neighbour->ha[4] != 0xff) {
  373. neigh_add_path(skb, dev);
  374. return;
  375. }
  376. /* Add in the P_Key for multicasts */
  377. skb->dst->neighbour->ha[8] = (priv->pkey >> 8) & 0xff;
  378. skb->dst->neighbour->ha[9] = priv->pkey & 0xff;
  379. ipoib_mcast_send(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4), skb);
  380. }
  381. static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
  382. struct ipoib_pseudoheader *phdr)
  383. {
  384. struct ipoib_dev_priv *priv = netdev_priv(dev);
  385. struct ipoib_path *path;
  386. /*
  387. * We can only be called from ipoib_start_xmit, so we're
  388. * inside tx_lock -- no need to save/restore flags.
  389. */
  390. spin_lock(&priv->lock);
  391. path = __path_find(dev, (union ib_gid *) (phdr->hwaddr + 4));
  392. if (!path) {
  393. path = path_rec_create(dev,
  394. (union ib_gid *) (phdr->hwaddr + 4));
  395. if (path) {
  396. /* put pseudoheader back on for next time */
  397. skb_push(skb, sizeof *phdr);
  398. __skb_queue_tail(&path->queue, skb);
  399. if (path_rec_start(dev, path)) {
  400. spin_unlock(&priv->lock);
  401. path_free(dev, path);
  402. return;
  403. } else
  404. __path_add(dev, path);
  405. } else {
  406. ++priv->stats.tx_dropped;
  407. dev_kfree_skb_any(skb);
  408. }
  409. spin_unlock(&priv->lock);
  410. return;
  411. }
  412. if (path->pathrec.dlid) {
  413. ipoib_dbg(priv, "Send unicast ARP to %04x\n",
  414. be16_to_cpu(path->pathrec.dlid));
  415. ipoib_send(dev, skb, path->ah,
  416. be32_to_cpup((__be32 *) phdr->hwaddr));
  417. } else if ((path->query || !path_rec_start(dev, path)) &&
  418. skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  419. /* put pseudoheader back on for next time */
  420. skb_push(skb, sizeof *phdr);
  421. __skb_queue_tail(&path->queue, skb);
  422. } else {
  423. ++priv->stats.tx_dropped;
  424. dev_kfree_skb_any(skb);
  425. }
  426. spin_unlock(&priv->lock);
  427. }
  428. static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
  429. {
  430. struct ipoib_dev_priv *priv = netdev_priv(dev);
  431. struct ipoib_neigh *neigh;
  432. unsigned long flags;
  433. if (!spin_trylock_irqsave(&priv->tx_lock, flags))
  434. return NETDEV_TX_LOCKED;
  435. /*
  436. * Check if our queue is stopped. Since we have the LLTX bit
  437. * set, we can't rely on netif_stop_queue() preventing our
  438. * xmit function from being called with a full queue.
  439. */
  440. if (unlikely(netif_queue_stopped(dev))) {
  441. spin_unlock_irqrestore(&priv->tx_lock, flags);
  442. return NETDEV_TX_BUSY;
  443. }
  444. if (skb->dst && skb->dst->neighbour) {
  445. if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) {
  446. ipoib_path_lookup(skb, dev);
  447. goto out;
  448. }
  449. neigh = *to_ipoib_neigh(skb->dst->neighbour);
  450. if (likely(neigh->ah)) {
  451. ipoib_send(dev, skb, neigh->ah,
  452. be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
  453. goto out;
  454. }
  455. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  456. spin_lock(&priv->lock);
  457. __skb_queue_tail(&neigh->queue, skb);
  458. spin_unlock(&priv->lock);
  459. } else {
  460. ++priv->stats.tx_dropped;
  461. dev_kfree_skb_any(skb);
  462. }
  463. } else {
  464. struct ipoib_pseudoheader *phdr =
  465. (struct ipoib_pseudoheader *) skb->data;
  466. skb_pull(skb, sizeof *phdr);
  467. if (phdr->hwaddr[4] == 0xff) {
  468. /* Add in the P_Key for multicast*/
  469. phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
  470. phdr->hwaddr[9] = priv->pkey & 0xff;
  471. ipoib_mcast_send(dev, (union ib_gid *) (phdr->hwaddr + 4), skb);
  472. } else {
  473. /* unicast GID -- should be ARP or RARP reply */
  474. if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
  475. (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
  476. ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x "
  477. IPOIB_GID_FMT "\n",
  478. skb->dst ? "neigh" : "dst",
  479. be16_to_cpup((__be16 *) skb->data),
  480. be32_to_cpup((__be32 *) phdr->hwaddr),
  481. IPOIB_GID_ARG(*(union ib_gid *) (phdr->hwaddr + 4)));
  482. dev_kfree_skb_any(skb);
  483. ++priv->stats.tx_dropped;
  484. goto out;
  485. }
  486. unicast_arp_send(skb, dev, phdr);
  487. }
  488. }
  489. out:
  490. spin_unlock_irqrestore(&priv->tx_lock, flags);
  491. return NETDEV_TX_OK;
  492. }
  493. static struct net_device_stats *ipoib_get_stats(struct net_device *dev)
  494. {
  495. struct ipoib_dev_priv *priv = netdev_priv(dev);
  496. return &priv->stats;
  497. }
  498. static void ipoib_timeout(struct net_device *dev)
  499. {
  500. struct ipoib_dev_priv *priv = netdev_priv(dev);
  501. ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
  502. jiffies_to_msecs(jiffies - dev->trans_start));
  503. ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
  504. netif_queue_stopped(dev),
  505. priv->tx_head, priv->tx_tail);
  506. /* XXX reset QP, etc. */
  507. }
  508. static int ipoib_hard_header(struct sk_buff *skb,
  509. struct net_device *dev,
  510. unsigned short type,
  511. void *daddr, void *saddr, unsigned len)
  512. {
  513. struct ipoib_header *header;
  514. header = (struct ipoib_header *) skb_push(skb, sizeof *header);
  515. header->proto = htons(type);
  516. header->reserved = 0;
  517. /*
  518. * If we don't have a neighbour structure, stuff the
  519. * destination address onto the front of the skb so we can
  520. * figure out where to send the packet later.
  521. */
  522. if (!skb->dst || !skb->dst->neighbour) {
  523. struct ipoib_pseudoheader *phdr =
  524. (struct ipoib_pseudoheader *) skb_push(skb, sizeof *phdr);
  525. memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
  526. }
  527. return 0;
  528. }
  529. static void ipoib_set_mcast_list(struct net_device *dev)
  530. {
  531. struct ipoib_dev_priv *priv = netdev_priv(dev);
  532. queue_work(ipoib_workqueue, &priv->restart_task);
  533. }
  534. static void ipoib_neigh_destructor(struct neighbour *n)
  535. {
  536. struct ipoib_neigh *neigh;
  537. struct ipoib_dev_priv *priv = netdev_priv(n->dev);
  538. unsigned long flags;
  539. struct ipoib_ah *ah = NULL;
  540. ipoib_dbg(priv,
  541. "neigh_destructor for %06x " IPOIB_GID_FMT "\n",
  542. be32_to_cpup((__be32 *) n->ha),
  543. IPOIB_GID_ARG(*((union ib_gid *) (n->ha + 4))));
  544. spin_lock_irqsave(&priv->lock, flags);
  545. neigh = *to_ipoib_neigh(n);
  546. if (neigh) {
  547. if (neigh->ah)
  548. ah = neigh->ah;
  549. list_del(&neigh->list);
  550. *to_ipoib_neigh(n) = NULL;
  551. kfree(neigh);
  552. }
  553. spin_unlock_irqrestore(&priv->lock, flags);
  554. if (ah)
  555. ipoib_put_ah(ah);
  556. }
  557. static int ipoib_neigh_setup(struct neighbour *neigh)
  558. {
  559. /*
  560. * Is this kosher? I can't find anybody in the kernel that
  561. * sets neigh->destructor, so we should be able to set it here
  562. * without trouble.
  563. */
  564. neigh->ops->destructor = ipoib_neigh_destructor;
  565. return 0;
  566. }
  567. static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
  568. {
  569. parms->neigh_setup = ipoib_neigh_setup;
  570. return 0;
  571. }
  572. int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  573. {
  574. struct ipoib_dev_priv *priv = netdev_priv(dev);
  575. /* Allocate RX/TX "rings" to hold queued skbs */
  576. priv->rx_ring = kzalloc(IPOIB_RX_RING_SIZE * sizeof (struct ipoib_rx_buf),
  577. GFP_KERNEL);
  578. if (!priv->rx_ring) {
  579. printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
  580. ca->name, IPOIB_RX_RING_SIZE);
  581. goto out;
  582. }
  583. priv->tx_ring = kzalloc(IPOIB_TX_RING_SIZE * sizeof (struct ipoib_tx_buf),
  584. GFP_KERNEL);
  585. if (!priv->tx_ring) {
  586. printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
  587. ca->name, IPOIB_TX_RING_SIZE);
  588. goto out_rx_ring_cleanup;
  589. }
  590. /* priv->tx_head & tx_tail are already 0 */
  591. if (ipoib_ib_dev_init(dev, ca, port))
  592. goto out_tx_ring_cleanup;
  593. return 0;
  594. out_tx_ring_cleanup:
  595. kfree(priv->tx_ring);
  596. out_rx_ring_cleanup:
  597. kfree(priv->rx_ring);
  598. out:
  599. return -ENOMEM;
  600. }
  601. void ipoib_dev_cleanup(struct net_device *dev)
  602. {
  603. struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
  604. ipoib_delete_debug_file(dev);
  605. /* Delete any child interfaces first */
  606. list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
  607. unregister_netdev(cpriv->dev);
  608. ipoib_dev_cleanup(cpriv->dev);
  609. free_netdev(cpriv->dev);
  610. }
  611. ipoib_ib_dev_cleanup(dev);
  612. kfree(priv->rx_ring);
  613. kfree(priv->tx_ring);
  614. priv->rx_ring = NULL;
  615. priv->tx_ring = NULL;
  616. }
  617. static void ipoib_setup(struct net_device *dev)
  618. {
  619. struct ipoib_dev_priv *priv = netdev_priv(dev);
  620. dev->open = ipoib_open;
  621. dev->stop = ipoib_stop;
  622. dev->change_mtu = ipoib_change_mtu;
  623. dev->hard_start_xmit = ipoib_start_xmit;
  624. dev->get_stats = ipoib_get_stats;
  625. dev->tx_timeout = ipoib_timeout;
  626. dev->hard_header = ipoib_hard_header;
  627. dev->set_multicast_list = ipoib_set_mcast_list;
  628. dev->neigh_setup = ipoib_neigh_setup_dev;
  629. dev->watchdog_timeo = HZ;
  630. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  631. /*
  632. * We add in INFINIBAND_ALEN to allow for the destination
  633. * address "pseudoheader" for skbs without neighbour struct.
  634. */
  635. dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
  636. dev->addr_len = INFINIBAND_ALEN;
  637. dev->type = ARPHRD_INFINIBAND;
  638. dev->tx_queue_len = IPOIB_TX_RING_SIZE * 2;
  639. dev->features = NETIF_F_VLAN_CHALLENGED | NETIF_F_LLTX;
  640. /* MTU will be reset when mcast join happens */
  641. dev->mtu = IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN;
  642. priv->mcast_mtu = priv->admin_mtu = dev->mtu;
  643. memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
  644. netif_carrier_off(dev);
  645. SET_MODULE_OWNER(dev);
  646. priv->dev = dev;
  647. spin_lock_init(&priv->lock);
  648. spin_lock_init(&priv->tx_lock);
  649. init_MUTEX(&priv->mcast_mutex);
  650. init_MUTEX(&priv->vlan_mutex);
  651. INIT_LIST_HEAD(&priv->path_list);
  652. INIT_LIST_HEAD(&priv->child_intfs);
  653. INIT_LIST_HEAD(&priv->dead_ahs);
  654. INIT_LIST_HEAD(&priv->multicast_list);
  655. INIT_WORK(&priv->pkey_task, ipoib_pkey_poll, priv->dev);
  656. INIT_WORK(&priv->mcast_task, ipoib_mcast_join_task, priv->dev);
  657. INIT_WORK(&priv->flush_task, ipoib_ib_dev_flush, priv->dev);
  658. INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task, priv->dev);
  659. INIT_WORK(&priv->ah_reap_task, ipoib_reap_ah, priv->dev);
  660. }
  661. struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
  662. {
  663. struct net_device *dev;
  664. dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
  665. ipoib_setup);
  666. if (!dev)
  667. return NULL;
  668. return netdev_priv(dev);
  669. }
  670. static ssize_t show_pkey(struct class_device *cdev, char *buf)
  671. {
  672. struct ipoib_dev_priv *priv =
  673. netdev_priv(container_of(cdev, struct net_device, class_dev));
  674. return sprintf(buf, "0x%04x\n", priv->pkey);
  675. }
  676. static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
  677. static ssize_t create_child(struct class_device *cdev,
  678. const char *buf, size_t count)
  679. {
  680. int pkey;
  681. int ret;
  682. if (sscanf(buf, "%i", &pkey) != 1)
  683. return -EINVAL;
  684. if (pkey < 0 || pkey > 0xffff)
  685. return -EINVAL;
  686. /*
  687. * Set the full membership bit, so that we join the right
  688. * broadcast group, etc.
  689. */
  690. pkey |= 0x8000;
  691. ret = ipoib_vlan_add(container_of(cdev, struct net_device, class_dev),
  692. pkey);
  693. return ret ? ret : count;
  694. }
  695. static CLASS_DEVICE_ATTR(create_child, S_IWUGO, NULL, create_child);
  696. static ssize_t delete_child(struct class_device *cdev,
  697. const char *buf, size_t count)
  698. {
  699. int pkey;
  700. int ret;
  701. if (sscanf(buf, "%i", &pkey) != 1)
  702. return -EINVAL;
  703. if (pkey < 0 || pkey > 0xffff)
  704. return -EINVAL;
  705. ret = ipoib_vlan_delete(container_of(cdev, struct net_device, class_dev),
  706. pkey);
  707. return ret ? ret : count;
  708. }
  709. static CLASS_DEVICE_ATTR(delete_child, S_IWUGO, NULL, delete_child);
  710. int ipoib_add_pkey_attr(struct net_device *dev)
  711. {
  712. return class_device_create_file(&dev->class_dev,
  713. &class_device_attr_pkey);
  714. }
  715. static struct net_device *ipoib_add_port(const char *format,
  716. struct ib_device *hca, u8 port)
  717. {
  718. struct ipoib_dev_priv *priv;
  719. int result = -ENOMEM;
  720. priv = ipoib_intf_alloc(format);
  721. if (!priv)
  722. goto alloc_mem_failed;
  723. SET_NETDEV_DEV(priv->dev, hca->dma_device);
  724. result = ib_query_pkey(hca, port, 0, &priv->pkey);
  725. if (result) {
  726. printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
  727. hca->name, port, result);
  728. goto alloc_mem_failed;
  729. }
  730. /*
  731. * Set the full membership bit, so that we join the right
  732. * broadcast group, etc.
  733. */
  734. priv->pkey |= 0x8000;
  735. priv->dev->broadcast[8] = priv->pkey >> 8;
  736. priv->dev->broadcast[9] = priv->pkey & 0xff;
  737. result = ib_query_gid(hca, port, 0, &priv->local_gid);
  738. if (result) {
  739. printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
  740. hca->name, port, result);
  741. goto alloc_mem_failed;
  742. } else
  743. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  744. result = ipoib_dev_init(priv->dev, hca, port);
  745. if (result < 0) {
  746. printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
  747. hca->name, port, result);
  748. goto device_init_failed;
  749. }
  750. INIT_IB_EVENT_HANDLER(&priv->event_handler,
  751. priv->ca, ipoib_event);
  752. result = ib_register_event_handler(&priv->event_handler);
  753. if (result < 0) {
  754. printk(KERN_WARNING "%s: ib_register_event_handler failed for "
  755. "port %d (ret = %d)\n",
  756. hca->name, port, result);
  757. goto event_failed;
  758. }
  759. result = register_netdev(priv->dev);
  760. if (result) {
  761. printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
  762. hca->name, port, result);
  763. goto register_failed;
  764. }
  765. if (ipoib_create_debug_file(priv->dev))
  766. goto debug_failed;
  767. if (ipoib_add_pkey_attr(priv->dev))
  768. goto sysfs_failed;
  769. if (class_device_create_file(&priv->dev->class_dev,
  770. &class_device_attr_create_child))
  771. goto sysfs_failed;
  772. if (class_device_create_file(&priv->dev->class_dev,
  773. &class_device_attr_delete_child))
  774. goto sysfs_failed;
  775. return priv->dev;
  776. sysfs_failed:
  777. ipoib_delete_debug_file(priv->dev);
  778. debug_failed:
  779. unregister_netdev(priv->dev);
  780. register_failed:
  781. ib_unregister_event_handler(&priv->event_handler);
  782. flush_scheduled_work();
  783. event_failed:
  784. ipoib_dev_cleanup(priv->dev);
  785. device_init_failed:
  786. free_netdev(priv->dev);
  787. alloc_mem_failed:
  788. return ERR_PTR(result);
  789. }
  790. static void ipoib_add_one(struct ib_device *device)
  791. {
  792. struct list_head *dev_list;
  793. struct net_device *dev;
  794. struct ipoib_dev_priv *priv;
  795. int s, e, p;
  796. dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
  797. if (!dev_list)
  798. return;
  799. INIT_LIST_HEAD(dev_list);
  800. if (device->node_type == IB_NODE_SWITCH) {
  801. s = 0;
  802. e = 0;
  803. } else {
  804. s = 1;
  805. e = device->phys_port_cnt;
  806. }
  807. for (p = s; p <= e; ++p) {
  808. dev = ipoib_add_port("ib%d", device, p);
  809. if (!IS_ERR(dev)) {
  810. priv = netdev_priv(dev);
  811. list_add_tail(&priv->list, dev_list);
  812. }
  813. }
  814. ib_set_client_data(device, &ipoib_client, dev_list);
  815. }
  816. static void ipoib_remove_one(struct ib_device *device)
  817. {
  818. struct ipoib_dev_priv *priv, *tmp;
  819. struct list_head *dev_list;
  820. dev_list = ib_get_client_data(device, &ipoib_client);
  821. list_for_each_entry_safe(priv, tmp, dev_list, list) {
  822. ib_unregister_event_handler(&priv->event_handler);
  823. flush_scheduled_work();
  824. unregister_netdev(priv->dev);
  825. ipoib_dev_cleanup(priv->dev);
  826. free_netdev(priv->dev);
  827. }
  828. kfree(dev_list);
  829. }
  830. static int __init ipoib_init_module(void)
  831. {
  832. int ret;
  833. ret = ipoib_register_debugfs();
  834. if (ret)
  835. return ret;
  836. /*
  837. * We create our own workqueue mainly because we want to be
  838. * able to flush it when devices are being removed. We can't
  839. * use schedule_work()/flush_scheduled_work() because both
  840. * unregister_netdev() and linkwatch_event take the rtnl lock,
  841. * so flush_scheduled_work() can deadlock during device
  842. * removal.
  843. */
  844. ipoib_workqueue = create_singlethread_workqueue("ipoib");
  845. if (!ipoib_workqueue) {
  846. ret = -ENOMEM;
  847. goto err_fs;
  848. }
  849. ret = ib_register_client(&ipoib_client);
  850. if (ret)
  851. goto err_wq;
  852. return 0;
  853. err_wq:
  854. destroy_workqueue(ipoib_workqueue);
  855. err_fs:
  856. ipoib_unregister_debugfs();
  857. return ret;
  858. }
  859. static void __exit ipoib_cleanup_module(void)
  860. {
  861. ib_unregister_client(&ipoib_client);
  862. ipoib_unregister_debugfs();
  863. destroy_workqueue(ipoib_workqueue);
  864. }
  865. module_init(ipoib_init_module);
  866. module_exit(ipoib_cleanup_module);