ipoib_main.c 43 KB

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