ipoib_main.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  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. #include "ipoib.h"
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/slab.h>
  38. #include <linux/kernel.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/if_arp.h> /* For ARPHRD_xxx */
  41. #include <linux/ip.h>
  42. #include <linux/in.h>
  43. #include <linux/jhash.h>
  44. #include <net/arp.h>
  45. #define DRV_VERSION "1.0.0"
  46. const char ipoib_driver_version[] = DRV_VERSION;
  47. MODULE_AUTHOR("Roland Dreier");
  48. MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
  49. MODULE_LICENSE("Dual BSD/GPL");
  50. MODULE_VERSION(DRV_VERSION);
  51. int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
  52. int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
  53. module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
  54. MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
  55. module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
  56. MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
  57. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  58. int ipoib_debug_level;
  59. module_param_named(debug_level, ipoib_debug_level, int, 0644);
  60. MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
  61. #endif
  62. struct ipoib_path_iter {
  63. struct net_device *dev;
  64. struct ipoib_path path;
  65. };
  66. static const u8 ipv4_bcast_addr[] = {
  67. 0x00, 0xff, 0xff, 0xff,
  68. 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
  70. };
  71. struct workqueue_struct *ipoib_workqueue;
  72. struct ib_sa_client ipoib_sa_client;
  73. static void ipoib_add_one(struct ib_device *device);
  74. static void ipoib_remove_one(struct ib_device *device);
  75. static void ipoib_neigh_reclaim(struct rcu_head *rp);
  76. static struct ib_client ipoib_client = {
  77. .name = "ipoib",
  78. .add = ipoib_add_one,
  79. .remove = ipoib_remove_one
  80. };
  81. int ipoib_open(struct net_device *dev)
  82. {
  83. struct ipoib_dev_priv *priv = netdev_priv(dev);
  84. ipoib_dbg(priv, "bringing up interface\n");
  85. set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  86. if (ipoib_pkey_dev_delay_open(dev))
  87. return 0;
  88. if (ipoib_ib_dev_open(dev))
  89. goto err_disable;
  90. if (ipoib_ib_dev_up(dev))
  91. goto err_stop;
  92. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  93. struct ipoib_dev_priv *cpriv;
  94. /* Bring up any child interfaces too */
  95. mutex_lock(&priv->vlan_mutex);
  96. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  97. int flags;
  98. flags = cpriv->dev->flags;
  99. if (flags & IFF_UP)
  100. continue;
  101. dev_change_flags(cpriv->dev, flags | IFF_UP);
  102. }
  103. mutex_unlock(&priv->vlan_mutex);
  104. }
  105. netif_start_queue(dev);
  106. return 0;
  107. err_stop:
  108. ipoib_ib_dev_stop(dev, 1);
  109. err_disable:
  110. clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  111. return -EINVAL;
  112. }
  113. static int ipoib_stop(struct net_device *dev)
  114. {
  115. struct ipoib_dev_priv *priv = netdev_priv(dev);
  116. ipoib_dbg(priv, "stopping interface\n");
  117. clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  118. netif_stop_queue(dev);
  119. ipoib_ib_dev_down(dev, 1);
  120. ipoib_ib_dev_stop(dev, 0);
  121. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  122. struct ipoib_dev_priv *cpriv;
  123. /* Bring down any child interfaces too */
  124. mutex_lock(&priv->vlan_mutex);
  125. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  126. int flags;
  127. flags = cpriv->dev->flags;
  128. if (!(flags & IFF_UP))
  129. continue;
  130. dev_change_flags(cpriv->dev, flags & ~IFF_UP);
  131. }
  132. mutex_unlock(&priv->vlan_mutex);
  133. }
  134. return 0;
  135. }
  136. static void ipoib_uninit(struct net_device *dev)
  137. {
  138. ipoib_dev_cleanup(dev);
  139. }
  140. static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
  141. {
  142. struct ipoib_dev_priv *priv = netdev_priv(dev);
  143. if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
  144. features &= ~(NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
  145. return features;
  146. }
  147. static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
  148. {
  149. struct ipoib_dev_priv *priv = netdev_priv(dev);
  150. /* dev->mtu > 2K ==> connected mode */
  151. if (ipoib_cm_admin_enabled(dev)) {
  152. if (new_mtu > ipoib_cm_max_mtu(dev))
  153. return -EINVAL;
  154. if (new_mtu > priv->mcast_mtu)
  155. ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
  156. priv->mcast_mtu);
  157. dev->mtu = new_mtu;
  158. return 0;
  159. }
  160. if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
  161. return -EINVAL;
  162. priv->admin_mtu = new_mtu;
  163. dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
  164. return 0;
  165. }
  166. int ipoib_set_mode(struct net_device *dev, const char *buf)
  167. {
  168. struct ipoib_dev_priv *priv = netdev_priv(dev);
  169. /* flush paths if we switch modes so that connections are restarted */
  170. if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
  171. set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
  172. ipoib_warn(priv, "enabling connected mode "
  173. "will cause multicast packet drops\n");
  174. netdev_update_features(dev);
  175. rtnl_unlock();
  176. priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
  177. ipoib_flush_paths(dev);
  178. rtnl_lock();
  179. return 0;
  180. }
  181. if (!strcmp(buf, "datagram\n")) {
  182. clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
  183. netdev_update_features(dev);
  184. dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
  185. rtnl_unlock();
  186. ipoib_flush_paths(dev);
  187. rtnl_lock();
  188. return 0;
  189. }
  190. return -EINVAL;
  191. }
  192. static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
  193. {
  194. struct ipoib_dev_priv *priv = netdev_priv(dev);
  195. struct rb_node *n = priv->path_tree.rb_node;
  196. struct ipoib_path *path;
  197. int ret;
  198. while (n) {
  199. path = rb_entry(n, struct ipoib_path, rb_node);
  200. ret = memcmp(gid, path->pathrec.dgid.raw,
  201. sizeof (union ib_gid));
  202. if (ret < 0)
  203. n = n->rb_left;
  204. else if (ret > 0)
  205. n = n->rb_right;
  206. else
  207. return path;
  208. }
  209. return NULL;
  210. }
  211. static int __path_add(struct net_device *dev, struct ipoib_path *path)
  212. {
  213. struct ipoib_dev_priv *priv = netdev_priv(dev);
  214. struct rb_node **n = &priv->path_tree.rb_node;
  215. struct rb_node *pn = NULL;
  216. struct ipoib_path *tpath;
  217. int ret;
  218. while (*n) {
  219. pn = *n;
  220. tpath = rb_entry(pn, struct ipoib_path, rb_node);
  221. ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
  222. sizeof (union ib_gid));
  223. if (ret < 0)
  224. n = &pn->rb_left;
  225. else if (ret > 0)
  226. n = &pn->rb_right;
  227. else
  228. return -EEXIST;
  229. }
  230. rb_link_node(&path->rb_node, pn, n);
  231. rb_insert_color(&path->rb_node, &priv->path_tree);
  232. list_add_tail(&path->list, &priv->path_list);
  233. return 0;
  234. }
  235. static void path_free(struct net_device *dev, struct ipoib_path *path)
  236. {
  237. struct sk_buff *skb;
  238. while ((skb = __skb_dequeue(&path->queue)))
  239. dev_kfree_skb_irq(skb);
  240. ipoib_dbg(netdev_priv(dev), "path_free\n");
  241. /* remove all neigh connected to this path */
  242. ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
  243. if (path->ah)
  244. ipoib_put_ah(path->ah);
  245. kfree(path);
  246. }
  247. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  248. struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
  249. {
  250. struct ipoib_path_iter *iter;
  251. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  252. if (!iter)
  253. return NULL;
  254. iter->dev = dev;
  255. memset(iter->path.pathrec.dgid.raw, 0, 16);
  256. if (ipoib_path_iter_next(iter)) {
  257. kfree(iter);
  258. return NULL;
  259. }
  260. return iter;
  261. }
  262. int ipoib_path_iter_next(struct ipoib_path_iter *iter)
  263. {
  264. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  265. struct rb_node *n;
  266. struct ipoib_path *path;
  267. int ret = 1;
  268. spin_lock_irq(&priv->lock);
  269. n = rb_first(&priv->path_tree);
  270. while (n) {
  271. path = rb_entry(n, struct ipoib_path, rb_node);
  272. if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
  273. sizeof (union ib_gid)) < 0) {
  274. iter->path = *path;
  275. ret = 0;
  276. break;
  277. }
  278. n = rb_next(n);
  279. }
  280. spin_unlock_irq(&priv->lock);
  281. return ret;
  282. }
  283. void ipoib_path_iter_read(struct ipoib_path_iter *iter,
  284. struct ipoib_path *path)
  285. {
  286. *path = iter->path;
  287. }
  288. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
  289. void ipoib_mark_paths_invalid(struct net_device *dev)
  290. {
  291. struct ipoib_dev_priv *priv = netdev_priv(dev);
  292. struct ipoib_path *path, *tp;
  293. spin_lock_irq(&priv->lock);
  294. list_for_each_entry_safe(path, tp, &priv->path_list, list) {
  295. ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
  296. be16_to_cpu(path->pathrec.dlid),
  297. path->pathrec.dgid.raw);
  298. path->valid = 0;
  299. }
  300. spin_unlock_irq(&priv->lock);
  301. }
  302. void ipoib_flush_paths(struct net_device *dev)
  303. {
  304. struct ipoib_dev_priv *priv = netdev_priv(dev);
  305. struct ipoib_path *path, *tp;
  306. LIST_HEAD(remove_list);
  307. unsigned long flags;
  308. netif_tx_lock_bh(dev);
  309. spin_lock_irqsave(&priv->lock, flags);
  310. list_splice_init(&priv->path_list, &remove_list);
  311. list_for_each_entry(path, &remove_list, list)
  312. rb_erase(&path->rb_node, &priv->path_tree);
  313. list_for_each_entry_safe(path, tp, &remove_list, list) {
  314. if (path->query)
  315. ib_sa_cancel_query(path->query_id, path->query);
  316. spin_unlock_irqrestore(&priv->lock, flags);
  317. netif_tx_unlock_bh(dev);
  318. wait_for_completion(&path->done);
  319. path_free(dev, path);
  320. netif_tx_lock_bh(dev);
  321. spin_lock_irqsave(&priv->lock, flags);
  322. }
  323. spin_unlock_irqrestore(&priv->lock, flags);
  324. netif_tx_unlock_bh(dev);
  325. }
  326. static void path_rec_completion(int status,
  327. struct ib_sa_path_rec *pathrec,
  328. void *path_ptr)
  329. {
  330. struct ipoib_path *path = path_ptr;
  331. struct net_device *dev = path->dev;
  332. struct ipoib_dev_priv *priv = netdev_priv(dev);
  333. struct ipoib_ah *ah = NULL;
  334. struct ipoib_ah *old_ah = NULL;
  335. struct ipoib_neigh *neigh, *tn;
  336. struct sk_buff_head skqueue;
  337. struct sk_buff *skb;
  338. unsigned long flags;
  339. if (!status)
  340. ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
  341. be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
  342. else
  343. ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
  344. status, path->pathrec.dgid.raw);
  345. skb_queue_head_init(&skqueue);
  346. if (!status) {
  347. struct ib_ah_attr av;
  348. if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
  349. ah = ipoib_create_ah(dev, priv->pd, &av);
  350. }
  351. spin_lock_irqsave(&priv->lock, flags);
  352. if (!IS_ERR_OR_NULL(ah)) {
  353. path->pathrec = *pathrec;
  354. old_ah = path->ah;
  355. path->ah = ah;
  356. ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
  357. ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
  358. while ((skb = __skb_dequeue(&path->queue)))
  359. __skb_queue_tail(&skqueue, skb);
  360. list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
  361. if (neigh->ah) {
  362. WARN_ON(neigh->ah != old_ah);
  363. /*
  364. * Dropping the ah reference inside
  365. * priv->lock is safe here, because we
  366. * will hold one more reference from
  367. * the original value of path->ah (ie
  368. * old_ah).
  369. */
  370. ipoib_put_ah(neigh->ah);
  371. }
  372. kref_get(&path->ah->ref);
  373. neigh->ah = path->ah;
  374. if (ipoib_cm_enabled(dev, neigh->daddr)) {
  375. if (!ipoib_cm_get(neigh))
  376. ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
  377. path,
  378. neigh));
  379. if (!ipoib_cm_get(neigh)) {
  380. list_del(&neigh->list);
  381. ipoib_neigh_free(neigh);
  382. continue;
  383. }
  384. }
  385. while ((skb = __skb_dequeue(&neigh->queue)))
  386. __skb_queue_tail(&skqueue, skb);
  387. }
  388. path->valid = 1;
  389. }
  390. path->query = NULL;
  391. complete(&path->done);
  392. spin_unlock_irqrestore(&priv->lock, flags);
  393. if (IS_ERR_OR_NULL(ah))
  394. ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
  395. if (old_ah)
  396. ipoib_put_ah(old_ah);
  397. while ((skb = __skb_dequeue(&skqueue))) {
  398. skb->dev = dev;
  399. if (dev_queue_xmit(skb))
  400. ipoib_warn(priv, "dev_queue_xmit failed "
  401. "to requeue packet\n");
  402. }
  403. }
  404. static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
  405. {
  406. struct ipoib_dev_priv *priv = netdev_priv(dev);
  407. struct ipoib_path *path;
  408. if (!priv->broadcast)
  409. return NULL;
  410. path = kzalloc(sizeof *path, GFP_ATOMIC);
  411. if (!path)
  412. return NULL;
  413. path->dev = dev;
  414. skb_queue_head_init(&path->queue);
  415. INIT_LIST_HEAD(&path->neigh_list);
  416. memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
  417. path->pathrec.sgid = priv->local_gid;
  418. path->pathrec.pkey = cpu_to_be16(priv->pkey);
  419. path->pathrec.numb_path = 1;
  420. path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
  421. return path;
  422. }
  423. static int path_rec_start(struct net_device *dev,
  424. struct ipoib_path *path)
  425. {
  426. struct ipoib_dev_priv *priv = netdev_priv(dev);
  427. ipoib_dbg(priv, "Start path record lookup for %pI6\n",
  428. path->pathrec.dgid.raw);
  429. init_completion(&path->done);
  430. path->query_id =
  431. ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
  432. &path->pathrec,
  433. IB_SA_PATH_REC_DGID |
  434. IB_SA_PATH_REC_SGID |
  435. IB_SA_PATH_REC_NUMB_PATH |
  436. IB_SA_PATH_REC_TRAFFIC_CLASS |
  437. IB_SA_PATH_REC_PKEY,
  438. 1000, GFP_ATOMIC,
  439. path_rec_completion,
  440. path, &path->query);
  441. if (path->query_id < 0) {
  442. ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
  443. path->query = NULL;
  444. complete(&path->done);
  445. return path->query_id;
  446. }
  447. return 0;
  448. }
  449. static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
  450. struct net_device *dev)
  451. {
  452. struct ipoib_dev_priv *priv = netdev_priv(dev);
  453. struct ipoib_path *path;
  454. struct ipoib_neigh *neigh;
  455. unsigned long flags;
  456. spin_lock_irqsave(&priv->lock, flags);
  457. neigh = ipoib_neigh_alloc(daddr, dev);
  458. if (!neigh) {
  459. spin_unlock_irqrestore(&priv->lock, flags);
  460. ++dev->stats.tx_dropped;
  461. dev_kfree_skb_any(skb);
  462. return;
  463. }
  464. path = __path_find(dev, daddr + 4);
  465. if (!path) {
  466. path = path_rec_create(dev, daddr + 4);
  467. if (!path)
  468. goto err_path;
  469. __path_add(dev, path);
  470. }
  471. list_add_tail(&neigh->list, &path->neigh_list);
  472. if (path->ah) {
  473. kref_get(&path->ah->ref);
  474. neigh->ah = path->ah;
  475. if (ipoib_cm_enabled(dev, neigh->daddr)) {
  476. if (!ipoib_cm_get(neigh))
  477. ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
  478. if (!ipoib_cm_get(neigh)) {
  479. list_del(&neigh->list);
  480. ipoib_neigh_free(neigh);
  481. goto err_drop;
  482. }
  483. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
  484. __skb_queue_tail(&neigh->queue, skb);
  485. else {
  486. ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
  487. skb_queue_len(&neigh->queue));
  488. goto err_drop;
  489. }
  490. } else {
  491. spin_unlock_irqrestore(&priv->lock, flags);
  492. ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
  493. ipoib_neigh_put(neigh);
  494. return;
  495. }
  496. } else {
  497. neigh->ah = NULL;
  498. if (!path->query && path_rec_start(dev, path))
  499. goto err_list;
  500. __skb_queue_tail(&neigh->queue, skb);
  501. }
  502. spin_unlock_irqrestore(&priv->lock, flags);
  503. ipoib_neigh_put(neigh);
  504. return;
  505. err_list:
  506. list_del(&neigh->list);
  507. err_path:
  508. ipoib_neigh_free(neigh);
  509. err_drop:
  510. ++dev->stats.tx_dropped;
  511. dev_kfree_skb_any(skb);
  512. spin_unlock_irqrestore(&priv->lock, flags);
  513. ipoib_neigh_put(neigh);
  514. }
  515. static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
  516. struct ipoib_cb *cb)
  517. {
  518. struct ipoib_dev_priv *priv = netdev_priv(dev);
  519. struct ipoib_path *path;
  520. unsigned long flags;
  521. spin_lock_irqsave(&priv->lock, flags);
  522. path = __path_find(dev, cb->hwaddr + 4);
  523. if (!path || !path->valid) {
  524. int new_path = 0;
  525. if (!path) {
  526. path = path_rec_create(dev, cb->hwaddr + 4);
  527. new_path = 1;
  528. }
  529. if (path) {
  530. __skb_queue_tail(&path->queue, skb);
  531. if (!path->query && path_rec_start(dev, path)) {
  532. spin_unlock_irqrestore(&priv->lock, flags);
  533. if (new_path)
  534. path_free(dev, path);
  535. return;
  536. } else
  537. __path_add(dev, path);
  538. } else {
  539. ++dev->stats.tx_dropped;
  540. dev_kfree_skb_any(skb);
  541. }
  542. spin_unlock_irqrestore(&priv->lock, flags);
  543. return;
  544. }
  545. if (path->ah) {
  546. ipoib_dbg(priv, "Send unicast ARP to %04x\n",
  547. be16_to_cpu(path->pathrec.dlid));
  548. spin_unlock_irqrestore(&priv->lock, flags);
  549. ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
  550. return;
  551. } else if ((path->query || !path_rec_start(dev, path)) &&
  552. skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  553. __skb_queue_tail(&path->queue, skb);
  554. } else {
  555. ++dev->stats.tx_dropped;
  556. dev_kfree_skb_any(skb);
  557. }
  558. spin_unlock_irqrestore(&priv->lock, flags);
  559. }
  560. static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
  561. {
  562. struct ipoib_dev_priv *priv = netdev_priv(dev);
  563. struct ipoib_neigh *neigh;
  564. struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
  565. struct ipoib_header *header;
  566. unsigned long flags;
  567. header = (struct ipoib_header *) skb->data;
  568. if (unlikely(cb->hwaddr[4] == 0xff)) {
  569. /* multicast, arrange "if" according to probability */
  570. if ((header->proto != htons(ETH_P_IP)) &&
  571. (header->proto != htons(ETH_P_IPV6)) &&
  572. (header->proto != htons(ETH_P_ARP)) &&
  573. (header->proto != htons(ETH_P_RARP))) {
  574. /* ethertype not supported by IPoIB */
  575. ++dev->stats.tx_dropped;
  576. dev_kfree_skb_any(skb);
  577. return NETDEV_TX_OK;
  578. }
  579. /* Add in the P_Key for multicast*/
  580. cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
  581. cb->hwaddr[9] = priv->pkey & 0xff;
  582. neigh = ipoib_neigh_get(dev, cb->hwaddr);
  583. if (likely(neigh))
  584. goto send_using_neigh;
  585. ipoib_mcast_send(dev, cb->hwaddr, skb);
  586. return NETDEV_TX_OK;
  587. }
  588. /* unicast, arrange "switch" according to probability */
  589. switch (header->proto) {
  590. case htons(ETH_P_IP):
  591. case htons(ETH_P_IPV6):
  592. neigh = ipoib_neigh_get(dev, cb->hwaddr);
  593. if (unlikely(!neigh)) {
  594. neigh_add_path(skb, cb->hwaddr, dev);
  595. return NETDEV_TX_OK;
  596. }
  597. break;
  598. case htons(ETH_P_ARP):
  599. case htons(ETH_P_RARP):
  600. /* for unicast ARP and RARP should always perform path find */
  601. unicast_arp_send(skb, dev, cb);
  602. return NETDEV_TX_OK;
  603. default:
  604. /* ethertype not supported by IPoIB */
  605. ++dev->stats.tx_dropped;
  606. dev_kfree_skb_any(skb);
  607. return NETDEV_TX_OK;
  608. }
  609. send_using_neigh:
  610. /* note we now hold a ref to neigh */
  611. if (ipoib_cm_get(neigh)) {
  612. if (ipoib_cm_up(neigh)) {
  613. ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
  614. goto unref;
  615. }
  616. } else if (neigh->ah) {
  617. ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
  618. goto unref;
  619. }
  620. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  621. spin_lock_irqsave(&priv->lock, flags);
  622. __skb_queue_tail(&neigh->queue, skb);
  623. spin_unlock_irqrestore(&priv->lock, flags);
  624. } else {
  625. ++dev->stats.tx_dropped;
  626. dev_kfree_skb_any(skb);
  627. }
  628. unref:
  629. ipoib_neigh_put(neigh);
  630. return NETDEV_TX_OK;
  631. }
  632. static void ipoib_timeout(struct net_device *dev)
  633. {
  634. struct ipoib_dev_priv *priv = netdev_priv(dev);
  635. ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
  636. jiffies_to_msecs(jiffies - dev->trans_start));
  637. ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
  638. netif_queue_stopped(dev),
  639. priv->tx_head, priv->tx_tail);
  640. /* XXX reset QP, etc. */
  641. }
  642. static int ipoib_hard_header(struct sk_buff *skb,
  643. struct net_device *dev,
  644. unsigned short type,
  645. const void *daddr, const void *saddr, unsigned len)
  646. {
  647. struct ipoib_header *header;
  648. struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
  649. header = (struct ipoib_header *) skb_push(skb, sizeof *header);
  650. header->proto = htons(type);
  651. header->reserved = 0;
  652. /*
  653. * we don't rely on dst_entry structure, always stuff the
  654. * destination address into skb->cb so we can figure out where
  655. * to send the packet later.
  656. */
  657. memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
  658. return 0;
  659. }
  660. static void ipoib_set_mcast_list(struct net_device *dev)
  661. {
  662. struct ipoib_dev_priv *priv = netdev_priv(dev);
  663. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  664. ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
  665. return;
  666. }
  667. queue_work(ipoib_workqueue, &priv->restart_task);
  668. }
  669. static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
  670. {
  671. /*
  672. * Use only the address parts that contributes to spreading
  673. * The subnet prefix is not used as one can not connect to
  674. * same remote port (GUID) using the same remote QPN via two
  675. * different subnets.
  676. */
  677. /* qpn octets[1:4) & port GUID octets[12:20) */
  678. u32 *d32 = (u32 *) daddr;
  679. u32 hv;
  680. hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
  681. return hv & htbl->mask;
  682. }
  683. struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
  684. {
  685. struct ipoib_dev_priv *priv = netdev_priv(dev);
  686. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  687. struct ipoib_neigh_hash *htbl;
  688. struct ipoib_neigh *neigh = NULL;
  689. u32 hash_val;
  690. rcu_read_lock_bh();
  691. htbl = rcu_dereference_bh(ntbl->htbl);
  692. if (!htbl)
  693. goto out_unlock;
  694. hash_val = ipoib_addr_hash(htbl, daddr);
  695. for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
  696. neigh != NULL;
  697. neigh = rcu_dereference_bh(neigh->hnext)) {
  698. if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
  699. /* found, take one ref on behalf of the caller */
  700. if (!atomic_inc_not_zero(&neigh->refcnt)) {
  701. /* deleted */
  702. neigh = NULL;
  703. goto out_unlock;
  704. }
  705. neigh->alive = jiffies;
  706. goto out_unlock;
  707. }
  708. }
  709. out_unlock:
  710. rcu_read_unlock_bh();
  711. return neigh;
  712. }
  713. static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
  714. {
  715. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  716. struct ipoib_neigh_hash *htbl;
  717. unsigned long neigh_obsolete;
  718. unsigned long dt;
  719. unsigned long flags;
  720. int i;
  721. if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  722. return;
  723. spin_lock_irqsave(&priv->lock, flags);
  724. htbl = rcu_dereference_protected(ntbl->htbl,
  725. lockdep_is_held(&priv->lock));
  726. if (!htbl)
  727. goto out_unlock;
  728. /* neigh is obsolete if it was idle for two GC periods */
  729. dt = 2 * arp_tbl.gc_interval;
  730. neigh_obsolete = jiffies - dt;
  731. /* handle possible race condition */
  732. if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  733. goto out_unlock;
  734. for (i = 0; i < htbl->size; i++) {
  735. struct ipoib_neigh *neigh;
  736. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  737. while ((neigh = rcu_dereference_protected(*np,
  738. lockdep_is_held(&priv->lock))) != NULL) {
  739. /* was the neigh idle for two GC periods */
  740. if (time_after(neigh_obsolete, neigh->alive)) {
  741. rcu_assign_pointer(*np,
  742. rcu_dereference_protected(neigh->hnext,
  743. lockdep_is_held(&priv->lock)));
  744. /* remove from path/mc list */
  745. list_del(&neigh->list);
  746. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  747. } else {
  748. np = &neigh->hnext;
  749. }
  750. }
  751. }
  752. out_unlock:
  753. spin_unlock_irqrestore(&priv->lock, flags);
  754. }
  755. static void ipoib_reap_neigh(struct work_struct *work)
  756. {
  757. struct ipoib_dev_priv *priv =
  758. container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
  759. __ipoib_reap_neigh(priv);
  760. if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  761. queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
  762. arp_tbl.gc_interval);
  763. }
  764. static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
  765. struct net_device *dev)
  766. {
  767. struct ipoib_neigh *neigh;
  768. neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
  769. if (!neigh)
  770. return NULL;
  771. neigh->dev = dev;
  772. memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
  773. skb_queue_head_init(&neigh->queue);
  774. INIT_LIST_HEAD(&neigh->list);
  775. ipoib_cm_set(neigh, NULL);
  776. /* one ref on behalf of the caller */
  777. atomic_set(&neigh->refcnt, 1);
  778. return neigh;
  779. }
  780. struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
  781. struct net_device *dev)
  782. {
  783. struct ipoib_dev_priv *priv = netdev_priv(dev);
  784. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  785. struct ipoib_neigh_hash *htbl;
  786. struct ipoib_neigh *neigh;
  787. u32 hash_val;
  788. htbl = rcu_dereference_protected(ntbl->htbl,
  789. lockdep_is_held(&priv->lock));
  790. if (!htbl) {
  791. neigh = NULL;
  792. goto out_unlock;
  793. }
  794. /* need to add a new neigh, but maybe some other thread succeeded?
  795. * recalc hash, maybe hash resize took place so we do a search
  796. */
  797. hash_val = ipoib_addr_hash(htbl, daddr);
  798. for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
  799. lockdep_is_held(&priv->lock));
  800. neigh != NULL;
  801. neigh = rcu_dereference_protected(neigh->hnext,
  802. lockdep_is_held(&priv->lock))) {
  803. if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
  804. /* found, take one ref on behalf of the caller */
  805. if (!atomic_inc_not_zero(&neigh->refcnt)) {
  806. /* deleted */
  807. neigh = NULL;
  808. break;
  809. }
  810. neigh->alive = jiffies;
  811. goto out_unlock;
  812. }
  813. }
  814. neigh = ipoib_neigh_ctor(daddr, dev);
  815. if (!neigh)
  816. goto out_unlock;
  817. /* one ref on behalf of the hash table */
  818. atomic_inc(&neigh->refcnt);
  819. neigh->alive = jiffies;
  820. /* put in hash */
  821. rcu_assign_pointer(neigh->hnext,
  822. rcu_dereference_protected(htbl->buckets[hash_val],
  823. lockdep_is_held(&priv->lock)));
  824. rcu_assign_pointer(htbl->buckets[hash_val], neigh);
  825. atomic_inc(&ntbl->entries);
  826. out_unlock:
  827. return neigh;
  828. }
  829. void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
  830. {
  831. /* neigh reference count was dropprd to zero */
  832. struct net_device *dev = neigh->dev;
  833. struct ipoib_dev_priv *priv = netdev_priv(dev);
  834. struct sk_buff *skb;
  835. if (neigh->ah)
  836. ipoib_put_ah(neigh->ah);
  837. while ((skb = __skb_dequeue(&neigh->queue))) {
  838. ++dev->stats.tx_dropped;
  839. dev_kfree_skb_any(skb);
  840. }
  841. if (ipoib_cm_get(neigh))
  842. ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
  843. ipoib_dbg(netdev_priv(dev),
  844. "neigh free for %06x %pI6\n",
  845. IPOIB_QPN(neigh->daddr),
  846. neigh->daddr + 4);
  847. kfree(neigh);
  848. if (atomic_dec_and_test(&priv->ntbl.entries)) {
  849. if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
  850. complete(&priv->ntbl.flushed);
  851. }
  852. }
  853. static void ipoib_neigh_reclaim(struct rcu_head *rp)
  854. {
  855. /* Called as a result of removal from hash table */
  856. struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
  857. /* note TX context may hold another ref */
  858. ipoib_neigh_put(neigh);
  859. }
  860. void ipoib_neigh_free(struct ipoib_neigh *neigh)
  861. {
  862. struct net_device *dev = neigh->dev;
  863. struct ipoib_dev_priv *priv = netdev_priv(dev);
  864. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  865. struct ipoib_neigh_hash *htbl;
  866. struct ipoib_neigh __rcu **np;
  867. struct ipoib_neigh *n;
  868. u32 hash_val;
  869. htbl = rcu_dereference_protected(ntbl->htbl,
  870. lockdep_is_held(&priv->lock));
  871. if (!htbl)
  872. return;
  873. hash_val = ipoib_addr_hash(htbl, neigh->daddr);
  874. np = &htbl->buckets[hash_val];
  875. for (n = rcu_dereference_protected(*np,
  876. lockdep_is_held(&priv->lock));
  877. n != NULL;
  878. n = rcu_dereference_protected(*np,
  879. lockdep_is_held(&priv->lock))) {
  880. if (n == neigh) {
  881. /* found */
  882. rcu_assign_pointer(*np,
  883. rcu_dereference_protected(neigh->hnext,
  884. lockdep_is_held(&priv->lock)));
  885. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  886. return;
  887. } else {
  888. np = &n->hnext;
  889. }
  890. }
  891. }
  892. static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
  893. {
  894. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  895. struct ipoib_neigh_hash *htbl;
  896. struct ipoib_neigh **buckets;
  897. u32 size;
  898. clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
  899. ntbl->htbl = NULL;
  900. htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
  901. if (!htbl)
  902. return -ENOMEM;
  903. set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  904. size = roundup_pow_of_two(arp_tbl.gc_thresh3);
  905. buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
  906. if (!buckets) {
  907. kfree(htbl);
  908. return -ENOMEM;
  909. }
  910. htbl->size = size;
  911. htbl->mask = (size - 1);
  912. htbl->buckets = buckets;
  913. ntbl->htbl = htbl;
  914. htbl->ntbl = ntbl;
  915. atomic_set(&ntbl->entries, 0);
  916. /* start garbage collection */
  917. clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  918. queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
  919. arp_tbl.gc_interval);
  920. return 0;
  921. }
  922. static void neigh_hash_free_rcu(struct rcu_head *head)
  923. {
  924. struct ipoib_neigh_hash *htbl = container_of(head,
  925. struct ipoib_neigh_hash,
  926. rcu);
  927. struct ipoib_neigh __rcu **buckets = htbl->buckets;
  928. struct ipoib_neigh_table *ntbl = htbl->ntbl;
  929. kfree(buckets);
  930. kfree(htbl);
  931. complete(&ntbl->deleted);
  932. }
  933. void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
  934. {
  935. struct ipoib_dev_priv *priv = netdev_priv(dev);
  936. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  937. struct ipoib_neigh_hash *htbl;
  938. unsigned long flags;
  939. int i;
  940. /* remove all neigh connected to a given path or mcast */
  941. spin_lock_irqsave(&priv->lock, flags);
  942. htbl = rcu_dereference_protected(ntbl->htbl,
  943. lockdep_is_held(&priv->lock));
  944. if (!htbl)
  945. goto out_unlock;
  946. for (i = 0; i < htbl->size; i++) {
  947. struct ipoib_neigh *neigh;
  948. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  949. while ((neigh = rcu_dereference_protected(*np,
  950. lockdep_is_held(&priv->lock))) != NULL) {
  951. /* delete neighs belong to this parent */
  952. if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
  953. rcu_assign_pointer(*np,
  954. rcu_dereference_protected(neigh->hnext,
  955. lockdep_is_held(&priv->lock)));
  956. /* remove from parent list */
  957. list_del(&neigh->list);
  958. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  959. } else {
  960. np = &neigh->hnext;
  961. }
  962. }
  963. }
  964. out_unlock:
  965. spin_unlock_irqrestore(&priv->lock, flags);
  966. }
  967. static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
  968. {
  969. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  970. struct ipoib_neigh_hash *htbl;
  971. unsigned long flags;
  972. int i, wait_flushed = 0;
  973. init_completion(&priv->ntbl.flushed);
  974. spin_lock_irqsave(&priv->lock, flags);
  975. htbl = rcu_dereference_protected(ntbl->htbl,
  976. lockdep_is_held(&priv->lock));
  977. if (!htbl)
  978. goto out_unlock;
  979. wait_flushed = atomic_read(&priv->ntbl.entries);
  980. if (!wait_flushed)
  981. goto free_htbl;
  982. for (i = 0; i < htbl->size; i++) {
  983. struct ipoib_neigh *neigh;
  984. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  985. while ((neigh = rcu_dereference_protected(*np,
  986. lockdep_is_held(&priv->lock))) != NULL) {
  987. rcu_assign_pointer(*np,
  988. rcu_dereference_protected(neigh->hnext,
  989. lockdep_is_held(&priv->lock)));
  990. /* remove from path/mc list */
  991. list_del(&neigh->list);
  992. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  993. }
  994. }
  995. free_htbl:
  996. rcu_assign_pointer(ntbl->htbl, NULL);
  997. call_rcu(&htbl->rcu, neigh_hash_free_rcu);
  998. out_unlock:
  999. spin_unlock_irqrestore(&priv->lock, flags);
  1000. if (wait_flushed)
  1001. wait_for_completion(&priv->ntbl.flushed);
  1002. }
  1003. static void ipoib_neigh_hash_uninit(struct net_device *dev)
  1004. {
  1005. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1006. int stopped;
  1007. ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
  1008. init_completion(&priv->ntbl.deleted);
  1009. set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
  1010. /* Stop GC if called at init fail need to cancel work */
  1011. stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1012. if (!stopped)
  1013. cancel_delayed_work(&priv->neigh_reap_task);
  1014. ipoib_flush_neighs(priv);
  1015. wait_for_completion(&priv->ntbl.deleted);
  1016. }
  1017. int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  1018. {
  1019. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1020. if (ipoib_neigh_hash_init(priv) < 0)
  1021. goto out;
  1022. /* Allocate RX/TX "rings" to hold queued skbs */
  1023. priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
  1024. GFP_KERNEL);
  1025. if (!priv->rx_ring) {
  1026. printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
  1027. ca->name, ipoib_recvq_size);
  1028. goto out_neigh_hash_cleanup;
  1029. }
  1030. priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
  1031. if (!priv->tx_ring) {
  1032. printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
  1033. ca->name, ipoib_sendq_size);
  1034. goto out_rx_ring_cleanup;
  1035. }
  1036. /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
  1037. if (ipoib_ib_dev_init(dev, ca, port))
  1038. goto out_tx_ring_cleanup;
  1039. return 0;
  1040. out_tx_ring_cleanup:
  1041. vfree(priv->tx_ring);
  1042. out_rx_ring_cleanup:
  1043. kfree(priv->rx_ring);
  1044. out_neigh_hash_cleanup:
  1045. ipoib_neigh_hash_uninit(dev);
  1046. out:
  1047. return -ENOMEM;
  1048. }
  1049. void ipoib_dev_cleanup(struct net_device *dev)
  1050. {
  1051. struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
  1052. LIST_HEAD(head);
  1053. ASSERT_RTNL();
  1054. ipoib_delete_debug_files(dev);
  1055. /* Delete any child interfaces first */
  1056. list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
  1057. /* Stop GC on child */
  1058. set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
  1059. cancel_delayed_work(&cpriv->neigh_reap_task);
  1060. unregister_netdevice_queue(cpriv->dev, &head);
  1061. }
  1062. unregister_netdevice_many(&head);
  1063. ipoib_ib_dev_cleanup(dev);
  1064. kfree(priv->rx_ring);
  1065. vfree(priv->tx_ring);
  1066. priv->rx_ring = NULL;
  1067. priv->tx_ring = NULL;
  1068. ipoib_neigh_hash_uninit(dev);
  1069. }
  1070. static const struct header_ops ipoib_header_ops = {
  1071. .create = ipoib_hard_header,
  1072. };
  1073. static const struct net_device_ops ipoib_netdev_ops = {
  1074. .ndo_uninit = ipoib_uninit,
  1075. .ndo_open = ipoib_open,
  1076. .ndo_stop = ipoib_stop,
  1077. .ndo_change_mtu = ipoib_change_mtu,
  1078. .ndo_fix_features = ipoib_fix_features,
  1079. .ndo_start_xmit = ipoib_start_xmit,
  1080. .ndo_tx_timeout = ipoib_timeout,
  1081. .ndo_set_rx_mode = ipoib_set_mcast_list,
  1082. };
  1083. void ipoib_setup(struct net_device *dev)
  1084. {
  1085. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1086. dev->netdev_ops = &ipoib_netdev_ops;
  1087. dev->header_ops = &ipoib_header_ops;
  1088. ipoib_set_ethtool_ops(dev);
  1089. netif_napi_add(dev, &priv->napi, ipoib_poll, 100);
  1090. dev->watchdog_timeo = HZ;
  1091. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  1092. dev->hard_header_len = IPOIB_ENCAP_LEN;
  1093. dev->addr_len = INFINIBAND_ALEN;
  1094. dev->type = ARPHRD_INFINIBAND;
  1095. dev->tx_queue_len = ipoib_sendq_size * 2;
  1096. dev->features = (NETIF_F_VLAN_CHALLENGED |
  1097. NETIF_F_HIGHDMA);
  1098. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1099. memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
  1100. netif_carrier_off(dev);
  1101. priv->dev = dev;
  1102. spin_lock_init(&priv->lock);
  1103. mutex_init(&priv->vlan_mutex);
  1104. INIT_LIST_HEAD(&priv->path_list);
  1105. INIT_LIST_HEAD(&priv->child_intfs);
  1106. INIT_LIST_HEAD(&priv->dead_ahs);
  1107. INIT_LIST_HEAD(&priv->multicast_list);
  1108. INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
  1109. INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
  1110. INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
  1111. INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
  1112. INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
  1113. INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
  1114. INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
  1115. INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
  1116. INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
  1117. }
  1118. struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
  1119. {
  1120. struct net_device *dev;
  1121. dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
  1122. ipoib_setup);
  1123. if (!dev)
  1124. return NULL;
  1125. return netdev_priv(dev);
  1126. }
  1127. static ssize_t show_pkey(struct device *dev,
  1128. struct device_attribute *attr, char *buf)
  1129. {
  1130. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  1131. return sprintf(buf, "0x%04x\n", priv->pkey);
  1132. }
  1133. static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
  1134. static ssize_t show_umcast(struct device *dev,
  1135. struct device_attribute *attr, char *buf)
  1136. {
  1137. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  1138. return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
  1139. }
  1140. void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
  1141. {
  1142. struct ipoib_dev_priv *priv = netdev_priv(ndev);
  1143. if (umcast_val > 0) {
  1144. set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  1145. ipoib_warn(priv, "ignoring multicast groups joined directly "
  1146. "by userspace\n");
  1147. } else
  1148. clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  1149. }
  1150. static ssize_t set_umcast(struct device *dev,
  1151. struct device_attribute *attr,
  1152. const char *buf, size_t count)
  1153. {
  1154. unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
  1155. ipoib_set_umcast(to_net_dev(dev), umcast_val);
  1156. return count;
  1157. }
  1158. static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
  1159. int ipoib_add_umcast_attr(struct net_device *dev)
  1160. {
  1161. return device_create_file(&dev->dev, &dev_attr_umcast);
  1162. }
  1163. static ssize_t create_child(struct device *dev,
  1164. struct device_attribute *attr,
  1165. const char *buf, size_t count)
  1166. {
  1167. int pkey;
  1168. int ret;
  1169. if (sscanf(buf, "%i", &pkey) != 1)
  1170. return -EINVAL;
  1171. if (pkey < 0 || pkey > 0xffff)
  1172. return -EINVAL;
  1173. /*
  1174. * Set the full membership bit, so that we join the right
  1175. * broadcast group, etc.
  1176. */
  1177. pkey |= 0x8000;
  1178. ret = ipoib_vlan_add(to_net_dev(dev), pkey);
  1179. return ret ? ret : count;
  1180. }
  1181. static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
  1182. static ssize_t delete_child(struct device *dev,
  1183. struct device_attribute *attr,
  1184. const char *buf, size_t count)
  1185. {
  1186. int pkey;
  1187. int ret;
  1188. if (sscanf(buf, "%i", &pkey) != 1)
  1189. return -EINVAL;
  1190. if (pkey < 0 || pkey > 0xffff)
  1191. return -EINVAL;
  1192. ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
  1193. return ret ? ret : count;
  1194. }
  1195. static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
  1196. int ipoib_add_pkey_attr(struct net_device *dev)
  1197. {
  1198. return device_create_file(&dev->dev, &dev_attr_pkey);
  1199. }
  1200. int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
  1201. {
  1202. struct ib_device_attr *device_attr;
  1203. int result = -ENOMEM;
  1204. device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
  1205. if (!device_attr) {
  1206. printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
  1207. hca->name, sizeof *device_attr);
  1208. return result;
  1209. }
  1210. result = ib_query_device(hca, device_attr);
  1211. if (result) {
  1212. printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
  1213. hca->name, result);
  1214. kfree(device_attr);
  1215. return result;
  1216. }
  1217. priv->hca_caps = device_attr->device_cap_flags;
  1218. kfree(device_attr);
  1219. if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
  1220. priv->dev->hw_features = NETIF_F_SG |
  1221. NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  1222. if (priv->hca_caps & IB_DEVICE_UD_TSO)
  1223. priv->dev->hw_features |= NETIF_F_TSO;
  1224. priv->dev->features |= priv->dev->hw_features;
  1225. }
  1226. return 0;
  1227. }
  1228. static struct net_device *ipoib_add_port(const char *format,
  1229. struct ib_device *hca, u8 port)
  1230. {
  1231. struct ipoib_dev_priv *priv;
  1232. struct ib_port_attr attr;
  1233. int result = -ENOMEM;
  1234. priv = ipoib_intf_alloc(format);
  1235. if (!priv)
  1236. goto alloc_mem_failed;
  1237. SET_NETDEV_DEV(priv->dev, hca->dma_device);
  1238. priv->dev->dev_id = port - 1;
  1239. if (!ib_query_port(hca, port, &attr))
  1240. priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
  1241. else {
  1242. printk(KERN_WARNING "%s: ib_query_port %d failed\n",
  1243. hca->name, port);
  1244. goto device_init_failed;
  1245. }
  1246. /* MTU will be reset when mcast join happens */
  1247. priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
  1248. priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
  1249. priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
  1250. result = ib_query_pkey(hca, port, 0, &priv->pkey);
  1251. if (result) {
  1252. printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
  1253. hca->name, port, result);
  1254. goto device_init_failed;
  1255. }
  1256. if (ipoib_set_dev_features(priv, hca))
  1257. goto device_init_failed;
  1258. /*
  1259. * Set the full membership bit, so that we join the right
  1260. * broadcast group, etc.
  1261. */
  1262. priv->pkey |= 0x8000;
  1263. priv->dev->broadcast[8] = priv->pkey >> 8;
  1264. priv->dev->broadcast[9] = priv->pkey & 0xff;
  1265. result = ib_query_gid(hca, port, 0, &priv->local_gid);
  1266. if (result) {
  1267. printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
  1268. hca->name, port, result);
  1269. goto device_init_failed;
  1270. } else
  1271. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  1272. result = ipoib_dev_init(priv->dev, hca, port);
  1273. if (result < 0) {
  1274. printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
  1275. hca->name, port, result);
  1276. goto device_init_failed;
  1277. }
  1278. INIT_IB_EVENT_HANDLER(&priv->event_handler,
  1279. priv->ca, ipoib_event);
  1280. result = ib_register_event_handler(&priv->event_handler);
  1281. if (result < 0) {
  1282. printk(KERN_WARNING "%s: ib_register_event_handler failed for "
  1283. "port %d (ret = %d)\n",
  1284. hca->name, port, result);
  1285. goto event_failed;
  1286. }
  1287. result = register_netdev(priv->dev);
  1288. if (result) {
  1289. printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
  1290. hca->name, port, result);
  1291. goto register_failed;
  1292. }
  1293. ipoib_create_debug_files(priv->dev);
  1294. if (ipoib_cm_add_mode_attr(priv->dev))
  1295. goto sysfs_failed;
  1296. if (ipoib_add_pkey_attr(priv->dev))
  1297. goto sysfs_failed;
  1298. if (ipoib_add_umcast_attr(priv->dev))
  1299. goto sysfs_failed;
  1300. if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
  1301. goto sysfs_failed;
  1302. if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
  1303. goto sysfs_failed;
  1304. return priv->dev;
  1305. sysfs_failed:
  1306. ipoib_delete_debug_files(priv->dev);
  1307. unregister_netdev(priv->dev);
  1308. register_failed:
  1309. ib_unregister_event_handler(&priv->event_handler);
  1310. /* Stop GC if started before flush */
  1311. set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1312. cancel_delayed_work(&priv->neigh_reap_task);
  1313. flush_workqueue(ipoib_workqueue);
  1314. event_failed:
  1315. ipoib_dev_cleanup(priv->dev);
  1316. device_init_failed:
  1317. free_netdev(priv->dev);
  1318. alloc_mem_failed:
  1319. return ERR_PTR(result);
  1320. }
  1321. static void ipoib_add_one(struct ib_device *device)
  1322. {
  1323. struct list_head *dev_list;
  1324. struct net_device *dev;
  1325. struct ipoib_dev_priv *priv;
  1326. int s, e, p;
  1327. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  1328. return;
  1329. dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
  1330. if (!dev_list)
  1331. return;
  1332. INIT_LIST_HEAD(dev_list);
  1333. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  1334. s = 0;
  1335. e = 0;
  1336. } else {
  1337. s = 1;
  1338. e = device->phys_port_cnt;
  1339. }
  1340. for (p = s; p <= e; ++p) {
  1341. if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
  1342. continue;
  1343. dev = ipoib_add_port("ib%d", device, p);
  1344. if (!IS_ERR(dev)) {
  1345. priv = netdev_priv(dev);
  1346. list_add_tail(&priv->list, dev_list);
  1347. }
  1348. }
  1349. ib_set_client_data(device, &ipoib_client, dev_list);
  1350. }
  1351. static void ipoib_remove_one(struct ib_device *device)
  1352. {
  1353. struct ipoib_dev_priv *priv, *tmp;
  1354. struct list_head *dev_list;
  1355. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  1356. return;
  1357. dev_list = ib_get_client_data(device, &ipoib_client);
  1358. if (!dev_list)
  1359. return;
  1360. list_for_each_entry_safe(priv, tmp, dev_list, list) {
  1361. ib_unregister_event_handler(&priv->event_handler);
  1362. rtnl_lock();
  1363. dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
  1364. rtnl_unlock();
  1365. /* Stop GC */
  1366. set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1367. cancel_delayed_work(&priv->neigh_reap_task);
  1368. flush_workqueue(ipoib_workqueue);
  1369. unregister_netdev(priv->dev);
  1370. free_netdev(priv->dev);
  1371. }
  1372. kfree(dev_list);
  1373. }
  1374. static int __init ipoib_init_module(void)
  1375. {
  1376. int ret;
  1377. ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
  1378. ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
  1379. ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
  1380. ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
  1381. ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
  1382. ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
  1383. #ifdef CONFIG_INFINIBAND_IPOIB_CM
  1384. ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
  1385. #endif
  1386. /*
  1387. * When copying small received packets, we only copy from the
  1388. * linear data part of the SKB, so we rely on this condition.
  1389. */
  1390. BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
  1391. ret = ipoib_register_debugfs();
  1392. if (ret)
  1393. return ret;
  1394. /*
  1395. * We create our own workqueue mainly because we want to be
  1396. * able to flush it when devices are being removed. We can't
  1397. * use schedule_work()/flush_scheduled_work() because both
  1398. * unregister_netdev() and linkwatch_event take the rtnl lock,
  1399. * so flush_scheduled_work() can deadlock during device
  1400. * removal.
  1401. */
  1402. ipoib_workqueue = create_singlethread_workqueue("ipoib");
  1403. if (!ipoib_workqueue) {
  1404. ret = -ENOMEM;
  1405. goto err_fs;
  1406. }
  1407. ib_sa_register_client(&ipoib_sa_client);
  1408. ret = ib_register_client(&ipoib_client);
  1409. if (ret)
  1410. goto err_sa;
  1411. ret = ipoib_netlink_init();
  1412. if (ret)
  1413. goto err_client;
  1414. return 0;
  1415. err_client:
  1416. ib_unregister_client(&ipoib_client);
  1417. err_sa:
  1418. ib_sa_unregister_client(&ipoib_sa_client);
  1419. destroy_workqueue(ipoib_workqueue);
  1420. err_fs:
  1421. ipoib_unregister_debugfs();
  1422. return ret;
  1423. }
  1424. static void __exit ipoib_cleanup_module(void)
  1425. {
  1426. ipoib_netlink_fini();
  1427. ib_unregister_client(&ipoib_client);
  1428. ib_sa_unregister_client(&ipoib_sa_client);
  1429. ipoib_unregister_debugfs();
  1430. destroy_workqueue(ipoib_workqueue);
  1431. }
  1432. module_init(ipoib_init_module);
  1433. module_exit(ipoib_cleanup_module);