ipoib_main.c 26 KB

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