ipoib_main.c 29 KB

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