ipoib_main.c 44 KB

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