ipoib_main.c 29 KB

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