ipoib_main.c 28 KB

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