ipoib_main.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794
  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. netif_carrier_off(dev);
  86. set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  87. if (ipoib_pkey_dev_delay_open(dev))
  88. return 0;
  89. if (ipoib_ib_dev_open(dev))
  90. goto err_disable;
  91. if (ipoib_ib_dev_up(dev))
  92. goto err_stop;
  93. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  94. struct ipoib_dev_priv *cpriv;
  95. /* Bring up any child interfaces too */
  96. down_read(&priv->vlan_rwsem);
  97. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  98. int flags;
  99. flags = cpriv->dev->flags;
  100. if (flags & IFF_UP)
  101. continue;
  102. dev_change_flags(cpriv->dev, flags | IFF_UP);
  103. }
  104. up_read(&priv->vlan_rwsem);
  105. }
  106. netif_start_queue(dev);
  107. return 0;
  108. err_stop:
  109. ipoib_ib_dev_stop(dev, 1);
  110. err_disable:
  111. clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  112. return -EINVAL;
  113. }
  114. static int ipoib_stop(struct net_device *dev)
  115. {
  116. struct ipoib_dev_priv *priv = netdev_priv(dev);
  117. ipoib_dbg(priv, "stopping interface\n");
  118. clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  119. netif_stop_queue(dev);
  120. ipoib_ib_dev_down(dev, 1);
  121. ipoib_ib_dev_stop(dev, 0);
  122. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  123. struct ipoib_dev_priv *cpriv;
  124. /* Bring down any child interfaces too */
  125. down_read(&priv->vlan_rwsem);
  126. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  127. int flags;
  128. flags = cpriv->dev->flags;
  129. if (!(flags & IFF_UP))
  130. continue;
  131. dev_change_flags(cpriv->dev, flags & ~IFF_UP);
  132. }
  133. up_read(&priv->vlan_rwsem);
  134. }
  135. return 0;
  136. }
  137. static void ipoib_uninit(struct net_device *dev)
  138. {
  139. ipoib_dev_cleanup(dev);
  140. }
  141. static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
  142. {
  143. struct ipoib_dev_priv *priv = netdev_priv(dev);
  144. if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
  145. features &= ~(NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
  146. return features;
  147. }
  148. static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
  149. {
  150. struct ipoib_dev_priv *priv = netdev_priv(dev);
  151. /* dev->mtu > 2K ==> connected mode */
  152. if (ipoib_cm_admin_enabled(dev)) {
  153. if (new_mtu > ipoib_cm_max_mtu(dev))
  154. return -EINVAL;
  155. if (new_mtu > priv->mcast_mtu)
  156. ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
  157. priv->mcast_mtu);
  158. dev->mtu = new_mtu;
  159. return 0;
  160. }
  161. if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
  162. return -EINVAL;
  163. priv->admin_mtu = new_mtu;
  164. dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
  165. return 0;
  166. }
  167. int ipoib_set_mode(struct net_device *dev, const char *buf)
  168. {
  169. struct ipoib_dev_priv *priv = netdev_priv(dev);
  170. /* flush paths if we switch modes so that connections are restarted */
  171. if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
  172. set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
  173. ipoib_warn(priv, "enabling connected mode "
  174. "will cause multicast packet drops\n");
  175. netdev_update_features(dev);
  176. rtnl_unlock();
  177. priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
  178. ipoib_flush_paths(dev);
  179. rtnl_lock();
  180. return 0;
  181. }
  182. if (!strcmp(buf, "datagram\n")) {
  183. clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
  184. netdev_update_features(dev);
  185. dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
  186. rtnl_unlock();
  187. ipoib_flush_paths(dev);
  188. rtnl_lock();
  189. return 0;
  190. }
  191. return -EINVAL;
  192. }
  193. static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
  194. {
  195. struct ipoib_dev_priv *priv = netdev_priv(dev);
  196. struct rb_node *n = priv->path_tree.rb_node;
  197. struct ipoib_path *path;
  198. int ret;
  199. while (n) {
  200. path = rb_entry(n, struct ipoib_path, rb_node);
  201. ret = memcmp(gid, path->pathrec.dgid.raw,
  202. sizeof (union ib_gid));
  203. if (ret < 0)
  204. n = n->rb_left;
  205. else if (ret > 0)
  206. n = n->rb_right;
  207. else
  208. return path;
  209. }
  210. return NULL;
  211. }
  212. static int __path_add(struct net_device *dev, struct ipoib_path *path)
  213. {
  214. struct ipoib_dev_priv *priv = netdev_priv(dev);
  215. struct rb_node **n = &priv->path_tree.rb_node;
  216. struct rb_node *pn = NULL;
  217. struct ipoib_path *tpath;
  218. int ret;
  219. while (*n) {
  220. pn = *n;
  221. tpath = rb_entry(pn, struct ipoib_path, rb_node);
  222. ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
  223. sizeof (union ib_gid));
  224. if (ret < 0)
  225. n = &pn->rb_left;
  226. else if (ret > 0)
  227. n = &pn->rb_right;
  228. else
  229. return -EEXIST;
  230. }
  231. rb_link_node(&path->rb_node, pn, n);
  232. rb_insert_color(&path->rb_node, &priv->path_tree);
  233. list_add_tail(&path->list, &priv->path_list);
  234. return 0;
  235. }
  236. static void path_free(struct net_device *dev, struct ipoib_path *path)
  237. {
  238. struct sk_buff *skb;
  239. while ((skb = __skb_dequeue(&path->queue)))
  240. dev_kfree_skb_irq(skb);
  241. ipoib_dbg(netdev_priv(dev), "path_free\n");
  242. /* remove all neigh connected to this path */
  243. ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
  244. if (path->ah)
  245. ipoib_put_ah(path->ah);
  246. kfree(path);
  247. }
  248. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  249. struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
  250. {
  251. struct ipoib_path_iter *iter;
  252. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  253. if (!iter)
  254. return NULL;
  255. iter->dev = dev;
  256. memset(iter->path.pathrec.dgid.raw, 0, 16);
  257. if (ipoib_path_iter_next(iter)) {
  258. kfree(iter);
  259. return NULL;
  260. }
  261. return iter;
  262. }
  263. int ipoib_path_iter_next(struct ipoib_path_iter *iter)
  264. {
  265. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  266. struct rb_node *n;
  267. struct ipoib_path *path;
  268. int ret = 1;
  269. spin_lock_irq(&priv->lock);
  270. n = rb_first(&priv->path_tree);
  271. while (n) {
  272. path = rb_entry(n, struct ipoib_path, rb_node);
  273. if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
  274. sizeof (union ib_gid)) < 0) {
  275. iter->path = *path;
  276. ret = 0;
  277. break;
  278. }
  279. n = rb_next(n);
  280. }
  281. spin_unlock_irq(&priv->lock);
  282. return ret;
  283. }
  284. void ipoib_path_iter_read(struct ipoib_path_iter *iter,
  285. struct ipoib_path *path)
  286. {
  287. *path = iter->path;
  288. }
  289. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
  290. void ipoib_mark_paths_invalid(struct net_device *dev)
  291. {
  292. struct ipoib_dev_priv *priv = netdev_priv(dev);
  293. struct ipoib_path *path, *tp;
  294. spin_lock_irq(&priv->lock);
  295. list_for_each_entry_safe(path, tp, &priv->path_list, list) {
  296. ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
  297. be16_to_cpu(path->pathrec.dlid),
  298. path->pathrec.dgid.raw);
  299. path->valid = 0;
  300. }
  301. spin_unlock_irq(&priv->lock);
  302. }
  303. void ipoib_flush_paths(struct net_device *dev)
  304. {
  305. struct ipoib_dev_priv *priv = netdev_priv(dev);
  306. struct ipoib_path *path, *tp;
  307. LIST_HEAD(remove_list);
  308. unsigned long flags;
  309. netif_tx_lock_bh(dev);
  310. spin_lock_irqsave(&priv->lock, flags);
  311. list_splice_init(&priv->path_list, &remove_list);
  312. list_for_each_entry(path, &remove_list, list)
  313. rb_erase(&path->rb_node, &priv->path_tree);
  314. list_for_each_entry_safe(path, tp, &remove_list, list) {
  315. if (path->query)
  316. ib_sa_cancel_query(path->query_id, path->query);
  317. spin_unlock_irqrestore(&priv->lock, flags);
  318. netif_tx_unlock_bh(dev);
  319. wait_for_completion(&path->done);
  320. path_free(dev, path);
  321. netif_tx_lock_bh(dev);
  322. spin_lock_irqsave(&priv->lock, flags);
  323. }
  324. spin_unlock_irqrestore(&priv->lock, flags);
  325. netif_tx_unlock_bh(dev);
  326. }
  327. static void path_rec_completion(int status,
  328. struct ib_sa_path_rec *pathrec,
  329. void *path_ptr)
  330. {
  331. struct ipoib_path *path = path_ptr;
  332. struct net_device *dev = path->dev;
  333. struct ipoib_dev_priv *priv = netdev_priv(dev);
  334. struct ipoib_ah *ah = NULL;
  335. struct ipoib_ah *old_ah = NULL;
  336. struct ipoib_neigh *neigh, *tn;
  337. struct sk_buff_head skqueue;
  338. struct sk_buff *skb;
  339. unsigned long flags;
  340. if (!status)
  341. ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
  342. be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
  343. else
  344. ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
  345. status, path->pathrec.dgid.raw);
  346. skb_queue_head_init(&skqueue);
  347. if (!status) {
  348. struct ib_ah_attr av;
  349. if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
  350. ah = ipoib_create_ah(dev, priv->pd, &av);
  351. }
  352. spin_lock_irqsave(&priv->lock, flags);
  353. if (!IS_ERR_OR_NULL(ah)) {
  354. path->pathrec = *pathrec;
  355. old_ah = path->ah;
  356. path->ah = ah;
  357. ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
  358. ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
  359. while ((skb = __skb_dequeue(&path->queue)))
  360. __skb_queue_tail(&skqueue, skb);
  361. list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
  362. if (neigh->ah) {
  363. WARN_ON(neigh->ah != old_ah);
  364. /*
  365. * Dropping the ah reference inside
  366. * priv->lock is safe here, because we
  367. * will hold one more reference from
  368. * the original value of path->ah (ie
  369. * old_ah).
  370. */
  371. ipoib_put_ah(neigh->ah);
  372. }
  373. kref_get(&path->ah->ref);
  374. neigh->ah = path->ah;
  375. if (ipoib_cm_enabled(dev, neigh->daddr)) {
  376. if (!ipoib_cm_get(neigh))
  377. ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
  378. path,
  379. neigh));
  380. if (!ipoib_cm_get(neigh)) {
  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. ipoib_neigh_free(neigh);
  480. goto err_drop;
  481. }
  482. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
  483. __skb_queue_tail(&neigh->queue, skb);
  484. else {
  485. ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
  486. skb_queue_len(&neigh->queue));
  487. goto err_drop;
  488. }
  489. } else {
  490. spin_unlock_irqrestore(&priv->lock, flags);
  491. ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
  492. ipoib_neigh_put(neigh);
  493. return;
  494. }
  495. } else {
  496. neigh->ah = NULL;
  497. if (!path->query && path_rec_start(dev, path))
  498. goto err_path;
  499. __skb_queue_tail(&neigh->queue, skb);
  500. }
  501. spin_unlock_irqrestore(&priv->lock, flags);
  502. ipoib_neigh_put(neigh);
  503. return;
  504. err_path:
  505. ipoib_neigh_free(neigh);
  506. err_drop:
  507. ++dev->stats.tx_dropped;
  508. dev_kfree_skb_any(skb);
  509. spin_unlock_irqrestore(&priv->lock, flags);
  510. ipoib_neigh_put(neigh);
  511. }
  512. static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
  513. struct ipoib_cb *cb)
  514. {
  515. struct ipoib_dev_priv *priv = netdev_priv(dev);
  516. struct ipoib_path *path;
  517. unsigned long flags;
  518. spin_lock_irqsave(&priv->lock, flags);
  519. path = __path_find(dev, cb->hwaddr + 4);
  520. if (!path || !path->valid) {
  521. int new_path = 0;
  522. if (!path) {
  523. path = path_rec_create(dev, cb->hwaddr + 4);
  524. new_path = 1;
  525. }
  526. if (path) {
  527. __skb_queue_tail(&path->queue, skb);
  528. if (!path->query && path_rec_start(dev, path)) {
  529. spin_unlock_irqrestore(&priv->lock, flags);
  530. if (new_path)
  531. path_free(dev, path);
  532. return;
  533. } else
  534. __path_add(dev, path);
  535. } else {
  536. ++dev->stats.tx_dropped;
  537. dev_kfree_skb_any(skb);
  538. }
  539. spin_unlock_irqrestore(&priv->lock, flags);
  540. return;
  541. }
  542. if (path->ah) {
  543. ipoib_dbg(priv, "Send unicast ARP to %04x\n",
  544. be16_to_cpu(path->pathrec.dlid));
  545. spin_unlock_irqrestore(&priv->lock, flags);
  546. ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
  547. return;
  548. } else if ((path->query || !path_rec_start(dev, path)) &&
  549. skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  550. __skb_queue_tail(&path->queue, skb);
  551. } else {
  552. ++dev->stats.tx_dropped;
  553. dev_kfree_skb_any(skb);
  554. }
  555. spin_unlock_irqrestore(&priv->lock, flags);
  556. }
  557. static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
  558. {
  559. struct ipoib_dev_priv *priv = netdev_priv(dev);
  560. struct ipoib_neigh *neigh;
  561. struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
  562. struct ipoib_header *header;
  563. unsigned long flags;
  564. header = (struct ipoib_header *) skb->data;
  565. if (unlikely(cb->hwaddr[4] == 0xff)) {
  566. /* multicast, arrange "if" according to probability */
  567. if ((header->proto != htons(ETH_P_IP)) &&
  568. (header->proto != htons(ETH_P_IPV6)) &&
  569. (header->proto != htons(ETH_P_ARP)) &&
  570. (header->proto != htons(ETH_P_RARP)) &&
  571. (header->proto != htons(ETH_P_TIPC))) {
  572. /* ethertype not supported by IPoIB */
  573. ++dev->stats.tx_dropped;
  574. dev_kfree_skb_any(skb);
  575. return NETDEV_TX_OK;
  576. }
  577. /* Add in the P_Key for multicast*/
  578. cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
  579. cb->hwaddr[9] = priv->pkey & 0xff;
  580. neigh = ipoib_neigh_get(dev, cb->hwaddr);
  581. if (likely(neigh))
  582. goto send_using_neigh;
  583. ipoib_mcast_send(dev, cb->hwaddr, skb);
  584. return NETDEV_TX_OK;
  585. }
  586. /* unicast, arrange "switch" according to probability */
  587. switch (header->proto) {
  588. case htons(ETH_P_IP):
  589. case htons(ETH_P_IPV6):
  590. case htons(ETH_P_TIPC):
  591. neigh = ipoib_neigh_get(dev, cb->hwaddr);
  592. if (unlikely(!neigh)) {
  593. neigh_add_path(skb, cb->hwaddr, dev);
  594. return NETDEV_TX_OK;
  595. }
  596. break;
  597. case htons(ETH_P_ARP):
  598. case htons(ETH_P_RARP):
  599. /* for unicast ARP and RARP should always perform path find */
  600. unicast_arp_send(skb, dev, cb);
  601. return NETDEV_TX_OK;
  602. default:
  603. /* ethertype not supported by IPoIB */
  604. ++dev->stats.tx_dropped;
  605. dev_kfree_skb_any(skb);
  606. return NETDEV_TX_OK;
  607. }
  608. send_using_neigh:
  609. /* note we now hold a ref to neigh */
  610. if (ipoib_cm_get(neigh)) {
  611. if (ipoib_cm_up(neigh)) {
  612. ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
  613. goto unref;
  614. }
  615. } else if (neigh->ah) {
  616. ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
  617. goto unref;
  618. }
  619. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  620. spin_lock_irqsave(&priv->lock, flags);
  621. __skb_queue_tail(&neigh->queue, skb);
  622. spin_unlock_irqrestore(&priv->lock, flags);
  623. } else {
  624. ++dev->stats.tx_dropped;
  625. dev_kfree_skb_any(skb);
  626. }
  627. unref:
  628. ipoib_neigh_put(neigh);
  629. return NETDEV_TX_OK;
  630. }
  631. static void ipoib_timeout(struct net_device *dev)
  632. {
  633. struct ipoib_dev_priv *priv = netdev_priv(dev);
  634. ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
  635. jiffies_to_msecs(jiffies - dev->trans_start));
  636. ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
  637. netif_queue_stopped(dev),
  638. priv->tx_head, priv->tx_tail);
  639. /* XXX reset QP, etc. */
  640. }
  641. static int ipoib_hard_header(struct sk_buff *skb,
  642. struct net_device *dev,
  643. unsigned short type,
  644. const void *daddr, const void *saddr, unsigned len)
  645. {
  646. struct ipoib_header *header;
  647. struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
  648. header = (struct ipoib_header *) skb_push(skb, sizeof *header);
  649. header->proto = htons(type);
  650. header->reserved = 0;
  651. /*
  652. * we don't rely on dst_entry structure, always stuff the
  653. * destination address into skb->cb so we can figure out where
  654. * to send the packet later.
  655. */
  656. memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
  657. return sizeof *header;
  658. }
  659. static void ipoib_set_mcast_list(struct net_device *dev)
  660. {
  661. struct ipoib_dev_priv *priv = netdev_priv(dev);
  662. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  663. ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
  664. return;
  665. }
  666. queue_work(ipoib_workqueue, &priv->restart_task);
  667. }
  668. static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
  669. {
  670. /*
  671. * Use only the address parts that contributes to spreading
  672. * The subnet prefix is not used as one can not connect to
  673. * same remote port (GUID) using the same remote QPN via two
  674. * different subnets.
  675. */
  676. /* qpn octets[1:4) & port GUID octets[12:20) */
  677. u32 *d32 = (u32 *) daddr;
  678. u32 hv;
  679. hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
  680. return hv & htbl->mask;
  681. }
  682. struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
  683. {
  684. struct ipoib_dev_priv *priv = netdev_priv(dev);
  685. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  686. struct ipoib_neigh_hash *htbl;
  687. struct ipoib_neigh *neigh = NULL;
  688. u32 hash_val;
  689. rcu_read_lock_bh();
  690. htbl = rcu_dereference_bh(ntbl->htbl);
  691. if (!htbl)
  692. goto out_unlock;
  693. hash_val = ipoib_addr_hash(htbl, daddr);
  694. for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
  695. neigh != NULL;
  696. neigh = rcu_dereference_bh(neigh->hnext)) {
  697. if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
  698. /* found, take one ref on behalf of the caller */
  699. if (!atomic_inc_not_zero(&neigh->refcnt)) {
  700. /* deleted */
  701. neigh = NULL;
  702. goto out_unlock;
  703. }
  704. neigh->alive = jiffies;
  705. goto out_unlock;
  706. }
  707. }
  708. out_unlock:
  709. rcu_read_unlock_bh();
  710. return neigh;
  711. }
  712. static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
  713. {
  714. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  715. struct ipoib_neigh_hash *htbl;
  716. unsigned long neigh_obsolete;
  717. unsigned long dt;
  718. unsigned long flags;
  719. int i;
  720. if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  721. return;
  722. spin_lock_irqsave(&priv->lock, flags);
  723. htbl = rcu_dereference_protected(ntbl->htbl,
  724. lockdep_is_held(&priv->lock));
  725. if (!htbl)
  726. goto out_unlock;
  727. /* neigh is obsolete if it was idle for two GC periods */
  728. dt = 2 * arp_tbl.gc_interval;
  729. neigh_obsolete = jiffies - dt;
  730. /* handle possible race condition */
  731. if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  732. goto out_unlock;
  733. for (i = 0; i < htbl->size; i++) {
  734. struct ipoib_neigh *neigh;
  735. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  736. while ((neigh = rcu_dereference_protected(*np,
  737. lockdep_is_held(&priv->lock))) != NULL) {
  738. /* was the neigh idle for two GC periods */
  739. if (time_after(neigh_obsolete, neigh->alive)) {
  740. rcu_assign_pointer(*np,
  741. rcu_dereference_protected(neigh->hnext,
  742. lockdep_is_held(&priv->lock)));
  743. /* remove from path/mc list */
  744. list_del(&neigh->list);
  745. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  746. } else {
  747. np = &neigh->hnext;
  748. }
  749. }
  750. }
  751. out_unlock:
  752. spin_unlock_irqrestore(&priv->lock, flags);
  753. }
  754. static void ipoib_reap_neigh(struct work_struct *work)
  755. {
  756. struct ipoib_dev_priv *priv =
  757. container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
  758. __ipoib_reap_neigh(priv);
  759. if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  760. queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
  761. arp_tbl.gc_interval);
  762. }
  763. static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
  764. struct net_device *dev)
  765. {
  766. struct ipoib_neigh *neigh;
  767. neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
  768. if (!neigh)
  769. return NULL;
  770. neigh->dev = dev;
  771. memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
  772. skb_queue_head_init(&neigh->queue);
  773. INIT_LIST_HEAD(&neigh->list);
  774. ipoib_cm_set(neigh, NULL);
  775. /* one ref on behalf of the caller */
  776. atomic_set(&neigh->refcnt, 1);
  777. return neigh;
  778. }
  779. struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
  780. struct net_device *dev)
  781. {
  782. struct ipoib_dev_priv *priv = netdev_priv(dev);
  783. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  784. struct ipoib_neigh_hash *htbl;
  785. struct ipoib_neigh *neigh;
  786. u32 hash_val;
  787. htbl = rcu_dereference_protected(ntbl->htbl,
  788. lockdep_is_held(&priv->lock));
  789. if (!htbl) {
  790. neigh = NULL;
  791. goto out_unlock;
  792. }
  793. /* need to add a new neigh, but maybe some other thread succeeded?
  794. * recalc hash, maybe hash resize took place so we do a search
  795. */
  796. hash_val = ipoib_addr_hash(htbl, daddr);
  797. for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
  798. lockdep_is_held(&priv->lock));
  799. neigh != NULL;
  800. neigh = rcu_dereference_protected(neigh->hnext,
  801. lockdep_is_held(&priv->lock))) {
  802. if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
  803. /* found, take one ref on behalf of the caller */
  804. if (!atomic_inc_not_zero(&neigh->refcnt)) {
  805. /* deleted */
  806. neigh = NULL;
  807. break;
  808. }
  809. neigh->alive = jiffies;
  810. goto out_unlock;
  811. }
  812. }
  813. neigh = ipoib_neigh_ctor(daddr, dev);
  814. if (!neigh)
  815. goto out_unlock;
  816. /* one ref on behalf of the hash table */
  817. atomic_inc(&neigh->refcnt);
  818. neigh->alive = jiffies;
  819. /* put in hash */
  820. rcu_assign_pointer(neigh->hnext,
  821. rcu_dereference_protected(htbl->buckets[hash_val],
  822. lockdep_is_held(&priv->lock)));
  823. rcu_assign_pointer(htbl->buckets[hash_val], neigh);
  824. atomic_inc(&ntbl->entries);
  825. out_unlock:
  826. return neigh;
  827. }
  828. void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
  829. {
  830. /* neigh reference count was dropprd to zero */
  831. struct net_device *dev = neigh->dev;
  832. struct ipoib_dev_priv *priv = netdev_priv(dev);
  833. struct sk_buff *skb;
  834. if (neigh->ah)
  835. ipoib_put_ah(neigh->ah);
  836. while ((skb = __skb_dequeue(&neigh->queue))) {
  837. ++dev->stats.tx_dropped;
  838. dev_kfree_skb_any(skb);
  839. }
  840. if (ipoib_cm_get(neigh))
  841. ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
  842. ipoib_dbg(netdev_priv(dev),
  843. "neigh free for %06x %pI6\n",
  844. IPOIB_QPN(neigh->daddr),
  845. neigh->daddr + 4);
  846. kfree(neigh);
  847. if (atomic_dec_and_test(&priv->ntbl.entries)) {
  848. if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
  849. complete(&priv->ntbl.flushed);
  850. }
  851. }
  852. static void ipoib_neigh_reclaim(struct rcu_head *rp)
  853. {
  854. /* Called as a result of removal from hash table */
  855. struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
  856. /* note TX context may hold another ref */
  857. ipoib_neigh_put(neigh);
  858. }
  859. void ipoib_neigh_free(struct ipoib_neigh *neigh)
  860. {
  861. struct net_device *dev = neigh->dev;
  862. struct ipoib_dev_priv *priv = netdev_priv(dev);
  863. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  864. struct ipoib_neigh_hash *htbl;
  865. struct ipoib_neigh __rcu **np;
  866. struct ipoib_neigh *n;
  867. u32 hash_val;
  868. htbl = rcu_dereference_protected(ntbl->htbl,
  869. lockdep_is_held(&priv->lock));
  870. if (!htbl)
  871. return;
  872. hash_val = ipoib_addr_hash(htbl, neigh->daddr);
  873. np = &htbl->buckets[hash_val];
  874. for (n = rcu_dereference_protected(*np,
  875. lockdep_is_held(&priv->lock));
  876. n != NULL;
  877. n = rcu_dereference_protected(*np,
  878. lockdep_is_held(&priv->lock))) {
  879. if (n == neigh) {
  880. /* found */
  881. rcu_assign_pointer(*np,
  882. rcu_dereference_protected(neigh->hnext,
  883. lockdep_is_held(&priv->lock)));
  884. /* remove from parent list */
  885. list_del(&neigh->list);
  886. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  887. return;
  888. } else {
  889. np = &n->hnext;
  890. }
  891. }
  892. }
  893. static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
  894. {
  895. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  896. struct ipoib_neigh_hash *htbl;
  897. struct ipoib_neigh **buckets;
  898. u32 size;
  899. clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
  900. ntbl->htbl = NULL;
  901. htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
  902. if (!htbl)
  903. return -ENOMEM;
  904. set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  905. size = roundup_pow_of_two(arp_tbl.gc_thresh3);
  906. buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
  907. if (!buckets) {
  908. kfree(htbl);
  909. return -ENOMEM;
  910. }
  911. htbl->size = size;
  912. htbl->mask = (size - 1);
  913. htbl->buckets = buckets;
  914. ntbl->htbl = htbl;
  915. htbl->ntbl = ntbl;
  916. atomic_set(&ntbl->entries, 0);
  917. /* start garbage collection */
  918. clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  919. queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
  920. arp_tbl.gc_interval);
  921. return 0;
  922. }
  923. static void neigh_hash_free_rcu(struct rcu_head *head)
  924. {
  925. struct ipoib_neigh_hash *htbl = container_of(head,
  926. struct ipoib_neigh_hash,
  927. rcu);
  928. struct ipoib_neigh __rcu **buckets = htbl->buckets;
  929. struct ipoib_neigh_table *ntbl = htbl->ntbl;
  930. kfree(buckets);
  931. kfree(htbl);
  932. complete(&ntbl->deleted);
  933. }
  934. void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
  935. {
  936. struct ipoib_dev_priv *priv = netdev_priv(dev);
  937. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  938. struct ipoib_neigh_hash *htbl;
  939. unsigned long flags;
  940. int i;
  941. /* remove all neigh connected to a given path or mcast */
  942. spin_lock_irqsave(&priv->lock, flags);
  943. htbl = rcu_dereference_protected(ntbl->htbl,
  944. lockdep_is_held(&priv->lock));
  945. if (!htbl)
  946. goto out_unlock;
  947. for (i = 0; i < htbl->size; i++) {
  948. struct ipoib_neigh *neigh;
  949. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  950. while ((neigh = rcu_dereference_protected(*np,
  951. lockdep_is_held(&priv->lock))) != NULL) {
  952. /* delete neighs belong to this parent */
  953. if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
  954. rcu_assign_pointer(*np,
  955. rcu_dereference_protected(neigh->hnext,
  956. lockdep_is_held(&priv->lock)));
  957. /* remove from parent list */
  958. list_del(&neigh->list);
  959. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  960. } else {
  961. np = &neigh->hnext;
  962. }
  963. }
  964. }
  965. out_unlock:
  966. spin_unlock_irqrestore(&priv->lock, flags);
  967. }
  968. static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
  969. {
  970. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  971. struct ipoib_neigh_hash *htbl;
  972. unsigned long flags;
  973. int i, wait_flushed = 0;
  974. init_completion(&priv->ntbl.flushed);
  975. spin_lock_irqsave(&priv->lock, flags);
  976. htbl = rcu_dereference_protected(ntbl->htbl,
  977. lockdep_is_held(&priv->lock));
  978. if (!htbl)
  979. goto out_unlock;
  980. wait_flushed = atomic_read(&priv->ntbl.entries);
  981. if (!wait_flushed)
  982. goto free_htbl;
  983. for (i = 0; i < htbl->size; i++) {
  984. struct ipoib_neigh *neigh;
  985. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  986. while ((neigh = rcu_dereference_protected(*np,
  987. lockdep_is_held(&priv->lock))) != NULL) {
  988. rcu_assign_pointer(*np,
  989. rcu_dereference_protected(neigh->hnext,
  990. lockdep_is_held(&priv->lock)));
  991. /* remove from path/mc list */
  992. list_del(&neigh->list);
  993. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  994. }
  995. }
  996. free_htbl:
  997. rcu_assign_pointer(ntbl->htbl, NULL);
  998. call_rcu(&htbl->rcu, neigh_hash_free_rcu);
  999. out_unlock:
  1000. spin_unlock_irqrestore(&priv->lock, flags);
  1001. if (wait_flushed)
  1002. wait_for_completion(&priv->ntbl.flushed);
  1003. }
  1004. static void ipoib_neigh_hash_uninit(struct net_device *dev)
  1005. {
  1006. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1007. int stopped;
  1008. ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
  1009. init_completion(&priv->ntbl.deleted);
  1010. set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
  1011. /* Stop GC if called at init fail need to cancel work */
  1012. stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1013. if (!stopped)
  1014. cancel_delayed_work(&priv->neigh_reap_task);
  1015. ipoib_flush_neighs(priv);
  1016. wait_for_completion(&priv->ntbl.deleted);
  1017. }
  1018. int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  1019. {
  1020. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1021. if (ipoib_neigh_hash_init(priv) < 0)
  1022. goto out;
  1023. /* Allocate RX/TX "rings" to hold queued skbs */
  1024. priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
  1025. GFP_KERNEL);
  1026. if (!priv->rx_ring) {
  1027. printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
  1028. ca->name, ipoib_recvq_size);
  1029. goto out_neigh_hash_cleanup;
  1030. }
  1031. priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
  1032. if (!priv->tx_ring) {
  1033. printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
  1034. ca->name, ipoib_sendq_size);
  1035. goto out_rx_ring_cleanup;
  1036. }
  1037. /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
  1038. if (ipoib_ib_dev_init(dev, ca, port))
  1039. goto out_tx_ring_cleanup;
  1040. return 0;
  1041. out_tx_ring_cleanup:
  1042. vfree(priv->tx_ring);
  1043. out_rx_ring_cleanup:
  1044. kfree(priv->rx_ring);
  1045. out_neigh_hash_cleanup:
  1046. ipoib_neigh_hash_uninit(dev);
  1047. out:
  1048. return -ENOMEM;
  1049. }
  1050. void ipoib_dev_cleanup(struct net_device *dev)
  1051. {
  1052. struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
  1053. LIST_HEAD(head);
  1054. ASSERT_RTNL();
  1055. ipoib_delete_debug_files(dev);
  1056. /* Delete any child interfaces first */
  1057. list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
  1058. /* Stop GC on child */
  1059. set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
  1060. cancel_delayed_work(&cpriv->neigh_reap_task);
  1061. unregister_netdevice_queue(cpriv->dev, &head);
  1062. }
  1063. unregister_netdevice_many(&head);
  1064. ipoib_ib_dev_cleanup(dev);
  1065. kfree(priv->rx_ring);
  1066. vfree(priv->tx_ring);
  1067. priv->rx_ring = NULL;
  1068. priv->tx_ring = NULL;
  1069. ipoib_neigh_hash_uninit(dev);
  1070. }
  1071. static const struct header_ops ipoib_header_ops = {
  1072. .create = ipoib_hard_header,
  1073. };
  1074. static const struct net_device_ops ipoib_netdev_ops = {
  1075. .ndo_uninit = ipoib_uninit,
  1076. .ndo_open = ipoib_open,
  1077. .ndo_stop = ipoib_stop,
  1078. .ndo_change_mtu = ipoib_change_mtu,
  1079. .ndo_fix_features = ipoib_fix_features,
  1080. .ndo_start_xmit = ipoib_start_xmit,
  1081. .ndo_tx_timeout = ipoib_timeout,
  1082. .ndo_set_rx_mode = ipoib_set_mcast_list,
  1083. };
  1084. void ipoib_setup(struct net_device *dev)
  1085. {
  1086. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1087. dev->netdev_ops = &ipoib_netdev_ops;
  1088. dev->header_ops = &ipoib_header_ops;
  1089. ipoib_set_ethtool_ops(dev);
  1090. netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
  1091. dev->watchdog_timeo = HZ;
  1092. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  1093. dev->hard_header_len = IPOIB_ENCAP_LEN;
  1094. dev->addr_len = INFINIBAND_ALEN;
  1095. dev->type = ARPHRD_INFINIBAND;
  1096. dev->tx_queue_len = ipoib_sendq_size * 2;
  1097. dev->features = (NETIF_F_VLAN_CHALLENGED |
  1098. NETIF_F_HIGHDMA);
  1099. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1100. memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
  1101. priv->dev = dev;
  1102. spin_lock_init(&priv->lock);
  1103. init_rwsem(&priv->vlan_rwsem);
  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 || pkey == 0x8000)
  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);