ipoib_main.c 44 KB

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