ipoib_main.c 28 KB

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