ipoib_main.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  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 (old_ah)
  394. ipoib_put_ah(old_ah);
  395. while ((skb = __skb_dequeue(&skqueue))) {
  396. skb->dev = dev;
  397. if (dev_queue_xmit(skb))
  398. ipoib_warn(priv, "dev_queue_xmit failed "
  399. "to requeue packet\n");
  400. }
  401. }
  402. static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
  403. {
  404. struct ipoib_dev_priv *priv = netdev_priv(dev);
  405. struct ipoib_path *path;
  406. if (!priv->broadcast)
  407. return NULL;
  408. path = kzalloc(sizeof *path, GFP_ATOMIC);
  409. if (!path)
  410. return NULL;
  411. path->dev = dev;
  412. skb_queue_head_init(&path->queue);
  413. INIT_LIST_HEAD(&path->neigh_list);
  414. memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
  415. path->pathrec.sgid = priv->local_gid;
  416. path->pathrec.pkey = cpu_to_be16(priv->pkey);
  417. path->pathrec.numb_path = 1;
  418. path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
  419. return path;
  420. }
  421. static int path_rec_start(struct net_device *dev,
  422. struct ipoib_path *path)
  423. {
  424. struct ipoib_dev_priv *priv = netdev_priv(dev);
  425. ipoib_dbg(priv, "Start path record lookup for %pI6\n",
  426. path->pathrec.dgid.raw);
  427. init_completion(&path->done);
  428. path->query_id =
  429. ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
  430. &path->pathrec,
  431. IB_SA_PATH_REC_DGID |
  432. IB_SA_PATH_REC_SGID |
  433. IB_SA_PATH_REC_NUMB_PATH |
  434. IB_SA_PATH_REC_TRAFFIC_CLASS |
  435. IB_SA_PATH_REC_PKEY,
  436. 1000, GFP_ATOMIC,
  437. path_rec_completion,
  438. path, &path->query);
  439. if (path->query_id < 0) {
  440. ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
  441. path->query = NULL;
  442. complete(&path->done);
  443. return path->query_id;
  444. }
  445. return 0;
  446. }
  447. static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
  448. struct net_device *dev)
  449. {
  450. struct ipoib_dev_priv *priv = netdev_priv(dev);
  451. struct ipoib_path *path;
  452. struct ipoib_neigh *neigh;
  453. unsigned long flags;
  454. spin_lock_irqsave(&priv->lock, flags);
  455. neigh = ipoib_neigh_alloc(daddr, dev);
  456. if (!neigh) {
  457. spin_unlock_irqrestore(&priv->lock, flags);
  458. ++dev->stats.tx_dropped;
  459. dev_kfree_skb_any(skb);
  460. return;
  461. }
  462. path = __path_find(dev, daddr + 4);
  463. if (!path) {
  464. path = path_rec_create(dev, daddr + 4);
  465. if (!path)
  466. goto err_path;
  467. __path_add(dev, path);
  468. }
  469. list_add_tail(&neigh->list, &path->neigh_list);
  470. if (path->ah) {
  471. kref_get(&path->ah->ref);
  472. neigh->ah = path->ah;
  473. if (ipoib_cm_enabled(dev, neigh->daddr)) {
  474. if (!ipoib_cm_get(neigh))
  475. ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
  476. if (!ipoib_cm_get(neigh)) {
  477. list_del(&neigh->list);
  478. ipoib_neigh_free(neigh);
  479. goto err_drop;
  480. }
  481. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
  482. __skb_queue_tail(&neigh->queue, skb);
  483. else {
  484. ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
  485. skb_queue_len(&neigh->queue));
  486. goto err_drop;
  487. }
  488. } else {
  489. spin_unlock_irqrestore(&priv->lock, flags);
  490. ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
  491. ipoib_neigh_put(neigh);
  492. return;
  493. }
  494. } else {
  495. neigh->ah = NULL;
  496. if (!path->query && path_rec_start(dev, path))
  497. goto err_list;
  498. __skb_queue_tail(&neigh->queue, skb);
  499. }
  500. spin_unlock_irqrestore(&priv->lock, flags);
  501. ipoib_neigh_put(neigh);
  502. return;
  503. err_list:
  504. list_del(&neigh->list);
  505. err_path:
  506. ipoib_neigh_free(neigh);
  507. err_drop:
  508. ++dev->stats.tx_dropped;
  509. dev_kfree_skb_any(skb);
  510. spin_unlock_irqrestore(&priv->lock, flags);
  511. ipoib_neigh_put(neigh);
  512. }
  513. static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
  514. struct ipoib_cb *cb)
  515. {
  516. struct ipoib_dev_priv *priv = netdev_priv(dev);
  517. struct ipoib_path *path;
  518. unsigned long flags;
  519. spin_lock_irqsave(&priv->lock, flags);
  520. path = __path_find(dev, cb->hwaddr + 4);
  521. if (!path || !path->valid) {
  522. int new_path = 0;
  523. if (!path) {
  524. path = path_rec_create(dev, cb->hwaddr + 4);
  525. new_path = 1;
  526. }
  527. if (path) {
  528. __skb_queue_tail(&path->queue, skb);
  529. if (!path->query && path_rec_start(dev, path)) {
  530. spin_unlock_irqrestore(&priv->lock, flags);
  531. if (new_path)
  532. path_free(dev, path);
  533. return;
  534. } else
  535. __path_add(dev, path);
  536. } else {
  537. ++dev->stats.tx_dropped;
  538. dev_kfree_skb_any(skb);
  539. }
  540. spin_unlock_irqrestore(&priv->lock, flags);
  541. return;
  542. }
  543. if (path->ah) {
  544. ipoib_dbg(priv, "Send unicast ARP to %04x\n",
  545. be16_to_cpu(path->pathrec.dlid));
  546. spin_unlock_irqrestore(&priv->lock, flags);
  547. ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
  548. return;
  549. } else if ((path->query || !path_rec_start(dev, path)) &&
  550. skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  551. __skb_queue_tail(&path->queue, skb);
  552. } else {
  553. ++dev->stats.tx_dropped;
  554. dev_kfree_skb_any(skb);
  555. }
  556. spin_unlock_irqrestore(&priv->lock, flags);
  557. }
  558. static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
  559. {
  560. struct ipoib_dev_priv *priv = netdev_priv(dev);
  561. struct ipoib_neigh *neigh;
  562. struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
  563. struct ipoib_header *header;
  564. unsigned long flags;
  565. header = (struct ipoib_header *) skb->data;
  566. if (unlikely(cb->hwaddr[4] == 0xff)) {
  567. /* multicast, arrange "if" according to probability */
  568. if ((header->proto != htons(ETH_P_IP)) &&
  569. (header->proto != htons(ETH_P_IPV6)) &&
  570. (header->proto != htons(ETH_P_ARP)) &&
  571. (header->proto != htons(ETH_P_RARP))) {
  572. /* ethertype not supported by IPoIB */
  573. ++dev->stats.tx_dropped;
  574. dev_kfree_skb_any(skb);
  575. return NETDEV_TX_OK;
  576. }
  577. /* Add in the P_Key for multicast*/
  578. cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
  579. cb->hwaddr[9] = priv->pkey & 0xff;
  580. neigh = ipoib_neigh_get(dev, cb->hwaddr);
  581. if (likely(neigh))
  582. goto send_using_neigh;
  583. ipoib_mcast_send(dev, cb->hwaddr, skb);
  584. return NETDEV_TX_OK;
  585. }
  586. /* unicast, arrange "switch" according to probability */
  587. switch (header->proto) {
  588. case htons(ETH_P_IP):
  589. case htons(ETH_P_IPV6):
  590. neigh = ipoib_neigh_get(dev, cb->hwaddr);
  591. if (unlikely(!neigh)) {
  592. neigh_add_path(skb, cb->hwaddr, dev);
  593. return NETDEV_TX_OK;
  594. }
  595. break;
  596. case htons(ETH_P_ARP):
  597. case htons(ETH_P_RARP):
  598. /* for unicast ARP and RARP should always perform path find */
  599. unicast_arp_send(skb, dev, cb);
  600. return NETDEV_TX_OK;
  601. default:
  602. /* ethertype not supported by IPoIB */
  603. ++dev->stats.tx_dropped;
  604. dev_kfree_skb_any(skb);
  605. return NETDEV_TX_OK;
  606. }
  607. send_using_neigh:
  608. /* note we now hold a ref to neigh */
  609. if (ipoib_cm_get(neigh)) {
  610. if (ipoib_cm_up(neigh)) {
  611. ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
  612. goto unref;
  613. }
  614. } else if (neigh->ah) {
  615. ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
  616. goto unref;
  617. }
  618. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  619. spin_lock_irqsave(&priv->lock, flags);
  620. __skb_queue_tail(&neigh->queue, skb);
  621. spin_unlock_irqrestore(&priv->lock, flags);
  622. } else {
  623. ++dev->stats.tx_dropped;
  624. dev_kfree_skb_any(skb);
  625. }
  626. unref:
  627. ipoib_neigh_put(neigh);
  628. return NETDEV_TX_OK;
  629. }
  630. static void ipoib_timeout(struct net_device *dev)
  631. {
  632. struct ipoib_dev_priv *priv = netdev_priv(dev);
  633. ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
  634. jiffies_to_msecs(jiffies - dev->trans_start));
  635. ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
  636. netif_queue_stopped(dev),
  637. priv->tx_head, priv->tx_tail);
  638. /* XXX reset QP, etc. */
  639. }
  640. static int ipoib_hard_header(struct sk_buff *skb,
  641. struct net_device *dev,
  642. unsigned short type,
  643. const void *daddr, const void *saddr, unsigned len)
  644. {
  645. struct ipoib_header *header;
  646. struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
  647. header = (struct ipoib_header *) skb_push(skb, sizeof *header);
  648. header->proto = htons(type);
  649. header->reserved = 0;
  650. /*
  651. * we don't rely on dst_entry structure, always stuff the
  652. * destination address into skb->cb so we can figure out where
  653. * to send the packet later.
  654. */
  655. memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
  656. return 0;
  657. }
  658. static void ipoib_set_mcast_list(struct net_device *dev)
  659. {
  660. struct ipoib_dev_priv *priv = netdev_priv(dev);
  661. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  662. ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
  663. return;
  664. }
  665. queue_work(ipoib_workqueue, &priv->restart_task);
  666. }
  667. static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
  668. {
  669. /*
  670. * Use only the address parts that contributes to spreading
  671. * The subnet prefix is not used as one can not connect to
  672. * same remote port (GUID) using the same remote QPN via two
  673. * different subnets.
  674. */
  675. /* qpn octets[1:4) & port GUID octets[12:20) */
  676. u32 *d32 = (u32 *) daddr;
  677. u32 hv;
  678. hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
  679. return hv & htbl->mask;
  680. }
  681. struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
  682. {
  683. struct ipoib_dev_priv *priv = netdev_priv(dev);
  684. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  685. struct ipoib_neigh_hash *htbl;
  686. struct ipoib_neigh *neigh = NULL;
  687. u32 hash_val;
  688. rcu_read_lock_bh();
  689. htbl = rcu_dereference_bh(ntbl->htbl);
  690. if (!htbl)
  691. goto out_unlock;
  692. hash_val = ipoib_addr_hash(htbl, daddr);
  693. for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
  694. neigh != NULL;
  695. neigh = rcu_dereference_bh(neigh->hnext)) {
  696. if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
  697. /* found, take one ref on behalf of the caller */
  698. if (!atomic_inc_not_zero(&neigh->refcnt)) {
  699. /* deleted */
  700. neigh = NULL;
  701. goto out_unlock;
  702. }
  703. neigh->alive = jiffies;
  704. goto out_unlock;
  705. }
  706. }
  707. out_unlock:
  708. rcu_read_unlock_bh();
  709. return neigh;
  710. }
  711. static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
  712. {
  713. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  714. struct ipoib_neigh_hash *htbl;
  715. unsigned long neigh_obsolete;
  716. unsigned long dt;
  717. unsigned long flags;
  718. int i;
  719. if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  720. return;
  721. spin_lock_irqsave(&priv->lock, flags);
  722. htbl = rcu_dereference_protected(ntbl->htbl,
  723. lockdep_is_held(&priv->lock));
  724. if (!htbl)
  725. goto out_unlock;
  726. /* neigh is obsolete if it was idle for two GC periods */
  727. dt = 2 * arp_tbl.gc_interval;
  728. neigh_obsolete = jiffies - dt;
  729. /* handle possible race condition */
  730. if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  731. goto out_unlock;
  732. for (i = 0; i < htbl->size; i++) {
  733. struct ipoib_neigh *neigh;
  734. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  735. while ((neigh = rcu_dereference_protected(*np,
  736. lockdep_is_held(&priv->lock))) != NULL) {
  737. /* was the neigh idle for two GC periods */
  738. if (time_after(neigh_obsolete, neigh->alive)) {
  739. rcu_assign_pointer(*np,
  740. rcu_dereference_protected(neigh->hnext,
  741. lockdep_is_held(&priv->lock)));
  742. /* remove from path/mc list */
  743. list_del(&neigh->list);
  744. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  745. } else {
  746. np = &neigh->hnext;
  747. }
  748. }
  749. }
  750. out_unlock:
  751. spin_unlock_irqrestore(&priv->lock, flags);
  752. }
  753. static void ipoib_reap_neigh(struct work_struct *work)
  754. {
  755. struct ipoib_dev_priv *priv =
  756. container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
  757. __ipoib_reap_neigh(priv);
  758. if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  759. queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
  760. arp_tbl.gc_interval);
  761. }
  762. static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
  763. struct net_device *dev)
  764. {
  765. struct ipoib_neigh *neigh;
  766. neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
  767. if (!neigh)
  768. return NULL;
  769. neigh->dev = dev;
  770. memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
  771. skb_queue_head_init(&neigh->queue);
  772. INIT_LIST_HEAD(&neigh->list);
  773. ipoib_cm_set(neigh, NULL);
  774. /* one ref on behalf of the caller */
  775. atomic_set(&neigh->refcnt, 1);
  776. return neigh;
  777. }
  778. struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
  779. struct net_device *dev)
  780. {
  781. struct ipoib_dev_priv *priv = netdev_priv(dev);
  782. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  783. struct ipoib_neigh_hash *htbl;
  784. struct ipoib_neigh *neigh;
  785. u32 hash_val;
  786. htbl = rcu_dereference_protected(ntbl->htbl,
  787. lockdep_is_held(&priv->lock));
  788. if (!htbl) {
  789. neigh = NULL;
  790. goto out_unlock;
  791. }
  792. /* need to add a new neigh, but maybe some other thread succeeded?
  793. * recalc hash, maybe hash resize took place so we do a search
  794. */
  795. hash_val = ipoib_addr_hash(htbl, daddr);
  796. for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
  797. lockdep_is_held(&priv->lock));
  798. neigh != NULL;
  799. neigh = rcu_dereference_protected(neigh->hnext,
  800. lockdep_is_held(&priv->lock))) {
  801. if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
  802. /* found, take one ref on behalf of the caller */
  803. if (!atomic_inc_not_zero(&neigh->refcnt)) {
  804. /* deleted */
  805. neigh = NULL;
  806. break;
  807. }
  808. neigh->alive = jiffies;
  809. goto out_unlock;
  810. }
  811. }
  812. neigh = ipoib_neigh_ctor(daddr, dev);
  813. if (!neigh)
  814. goto out_unlock;
  815. /* one ref on behalf of the hash table */
  816. atomic_inc(&neigh->refcnt);
  817. neigh->alive = jiffies;
  818. /* put in hash */
  819. rcu_assign_pointer(neigh->hnext,
  820. rcu_dereference_protected(htbl->buckets[hash_val],
  821. lockdep_is_held(&priv->lock)));
  822. rcu_assign_pointer(htbl->buckets[hash_val], neigh);
  823. atomic_inc(&ntbl->entries);
  824. out_unlock:
  825. return neigh;
  826. }
  827. void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
  828. {
  829. /* neigh reference count was dropprd to zero */
  830. struct net_device *dev = neigh->dev;
  831. struct ipoib_dev_priv *priv = netdev_priv(dev);
  832. struct sk_buff *skb;
  833. if (neigh->ah)
  834. ipoib_put_ah(neigh->ah);
  835. while ((skb = __skb_dequeue(&neigh->queue))) {
  836. ++dev->stats.tx_dropped;
  837. dev_kfree_skb_any(skb);
  838. }
  839. if (ipoib_cm_get(neigh))
  840. ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
  841. ipoib_dbg(netdev_priv(dev),
  842. "neigh free for %06x %pI6\n",
  843. IPOIB_QPN(neigh->daddr),
  844. neigh->daddr + 4);
  845. kfree(neigh);
  846. if (atomic_dec_and_test(&priv->ntbl.entries)) {
  847. if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
  848. complete(&priv->ntbl.flushed);
  849. }
  850. }
  851. static void ipoib_neigh_reclaim(struct rcu_head *rp)
  852. {
  853. /* Called as a result of removal from hash table */
  854. struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
  855. /* note TX context may hold another ref */
  856. ipoib_neigh_put(neigh);
  857. }
  858. void ipoib_neigh_free(struct ipoib_neigh *neigh)
  859. {
  860. struct net_device *dev = neigh->dev;
  861. struct ipoib_dev_priv *priv = netdev_priv(dev);
  862. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  863. struct ipoib_neigh_hash *htbl;
  864. struct ipoib_neigh __rcu **np;
  865. struct ipoib_neigh *n;
  866. u32 hash_val;
  867. htbl = rcu_dereference_protected(ntbl->htbl,
  868. lockdep_is_held(&priv->lock));
  869. if (!htbl)
  870. return;
  871. hash_val = ipoib_addr_hash(htbl, neigh->daddr);
  872. np = &htbl->buckets[hash_val];
  873. for (n = rcu_dereference_protected(*np,
  874. lockdep_is_held(&priv->lock));
  875. n != NULL;
  876. n = rcu_dereference_protected(*np,
  877. lockdep_is_held(&priv->lock))) {
  878. if (n == neigh) {
  879. /* found */
  880. rcu_assign_pointer(*np,
  881. rcu_dereference_protected(neigh->hnext,
  882. lockdep_is_held(&priv->lock)));
  883. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  884. return;
  885. } else {
  886. np = &n->hnext;
  887. }
  888. }
  889. }
  890. static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
  891. {
  892. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  893. struct ipoib_neigh_hash *htbl;
  894. struct ipoib_neigh **buckets;
  895. u32 size;
  896. clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
  897. ntbl->htbl = NULL;
  898. htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
  899. if (!htbl)
  900. return -ENOMEM;
  901. set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  902. size = roundup_pow_of_two(arp_tbl.gc_thresh3);
  903. buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
  904. if (!buckets) {
  905. kfree(htbl);
  906. return -ENOMEM;
  907. }
  908. htbl->size = size;
  909. htbl->mask = (size - 1);
  910. htbl->buckets = buckets;
  911. ntbl->htbl = htbl;
  912. htbl->ntbl = ntbl;
  913. atomic_set(&ntbl->entries, 0);
  914. /* start garbage collection */
  915. clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  916. queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
  917. arp_tbl.gc_interval);
  918. return 0;
  919. }
  920. static void neigh_hash_free_rcu(struct rcu_head *head)
  921. {
  922. struct ipoib_neigh_hash *htbl = container_of(head,
  923. struct ipoib_neigh_hash,
  924. rcu);
  925. struct ipoib_neigh __rcu **buckets = htbl->buckets;
  926. struct ipoib_neigh_table *ntbl = htbl->ntbl;
  927. kfree(buckets);
  928. kfree(htbl);
  929. complete(&ntbl->deleted);
  930. }
  931. void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
  932. {
  933. struct ipoib_dev_priv *priv = netdev_priv(dev);
  934. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  935. struct ipoib_neigh_hash *htbl;
  936. unsigned long flags;
  937. int i;
  938. /* remove all neigh connected to a given path or mcast */
  939. spin_lock_irqsave(&priv->lock, flags);
  940. htbl = rcu_dereference_protected(ntbl->htbl,
  941. lockdep_is_held(&priv->lock));
  942. if (!htbl)
  943. goto out_unlock;
  944. for (i = 0; i < htbl->size; i++) {
  945. struct ipoib_neigh *neigh;
  946. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  947. while ((neigh = rcu_dereference_protected(*np,
  948. lockdep_is_held(&priv->lock))) != NULL) {
  949. /* delete neighs belong to this parent */
  950. if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
  951. rcu_assign_pointer(*np,
  952. rcu_dereference_protected(neigh->hnext,
  953. lockdep_is_held(&priv->lock)));
  954. /* remove from parent list */
  955. list_del(&neigh->list);
  956. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  957. } else {
  958. np = &neigh->hnext;
  959. }
  960. }
  961. }
  962. out_unlock:
  963. spin_unlock_irqrestore(&priv->lock, flags);
  964. }
  965. static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
  966. {
  967. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  968. struct ipoib_neigh_hash *htbl;
  969. unsigned long flags;
  970. int i, wait_flushed = 0;
  971. init_completion(&priv->ntbl.flushed);
  972. spin_lock_irqsave(&priv->lock, flags);
  973. htbl = rcu_dereference_protected(ntbl->htbl,
  974. lockdep_is_held(&priv->lock));
  975. if (!htbl)
  976. goto out_unlock;
  977. wait_flushed = atomic_read(&priv->ntbl.entries);
  978. if (!wait_flushed)
  979. goto free_htbl;
  980. for (i = 0; i < htbl->size; i++) {
  981. struct ipoib_neigh *neigh;
  982. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  983. while ((neigh = rcu_dereference_protected(*np,
  984. lockdep_is_held(&priv->lock))) != NULL) {
  985. rcu_assign_pointer(*np,
  986. rcu_dereference_protected(neigh->hnext,
  987. lockdep_is_held(&priv->lock)));
  988. /* remove from path/mc list */
  989. list_del(&neigh->list);
  990. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  991. }
  992. }
  993. free_htbl:
  994. rcu_assign_pointer(ntbl->htbl, NULL);
  995. call_rcu(&htbl->rcu, neigh_hash_free_rcu);
  996. out_unlock:
  997. spin_unlock_irqrestore(&priv->lock, flags);
  998. if (wait_flushed)
  999. wait_for_completion(&priv->ntbl.flushed);
  1000. }
  1001. static void ipoib_neigh_hash_uninit(struct net_device *dev)
  1002. {
  1003. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1004. int stopped;
  1005. ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
  1006. init_completion(&priv->ntbl.deleted);
  1007. set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
  1008. /* Stop GC if called at init fail need to cancel work */
  1009. stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1010. if (!stopped)
  1011. cancel_delayed_work(&priv->neigh_reap_task);
  1012. ipoib_flush_neighs(priv);
  1013. wait_for_completion(&priv->ntbl.deleted);
  1014. }
  1015. int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  1016. {
  1017. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1018. if (ipoib_neigh_hash_init(priv) < 0)
  1019. goto out;
  1020. /* Allocate RX/TX "rings" to hold queued skbs */
  1021. priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
  1022. GFP_KERNEL);
  1023. if (!priv->rx_ring) {
  1024. printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
  1025. ca->name, ipoib_recvq_size);
  1026. goto out_neigh_hash_cleanup;
  1027. }
  1028. priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
  1029. if (!priv->tx_ring) {
  1030. printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
  1031. ca->name, ipoib_sendq_size);
  1032. goto out_rx_ring_cleanup;
  1033. }
  1034. /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
  1035. if (ipoib_ib_dev_init(dev, ca, port))
  1036. goto out_tx_ring_cleanup;
  1037. return 0;
  1038. out_tx_ring_cleanup:
  1039. vfree(priv->tx_ring);
  1040. out_rx_ring_cleanup:
  1041. kfree(priv->rx_ring);
  1042. out_neigh_hash_cleanup:
  1043. ipoib_neigh_hash_uninit(dev);
  1044. out:
  1045. return -ENOMEM;
  1046. }
  1047. void ipoib_dev_cleanup(struct net_device *dev)
  1048. {
  1049. struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
  1050. LIST_HEAD(head);
  1051. ASSERT_RTNL();
  1052. ipoib_delete_debug_files(dev);
  1053. /* Delete any child interfaces first */
  1054. list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
  1055. /* Stop GC on child */
  1056. set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
  1057. cancel_delayed_work(&cpriv->neigh_reap_task);
  1058. unregister_netdevice_queue(cpriv->dev, &head);
  1059. }
  1060. unregister_netdevice_many(&head);
  1061. ipoib_ib_dev_cleanup(dev);
  1062. kfree(priv->rx_ring);
  1063. vfree(priv->tx_ring);
  1064. priv->rx_ring = NULL;
  1065. priv->tx_ring = NULL;
  1066. ipoib_neigh_hash_uninit(dev);
  1067. }
  1068. static const struct header_ops ipoib_header_ops = {
  1069. .create = ipoib_hard_header,
  1070. };
  1071. static const struct net_device_ops ipoib_netdev_ops = {
  1072. .ndo_uninit = ipoib_uninit,
  1073. .ndo_open = ipoib_open,
  1074. .ndo_stop = ipoib_stop,
  1075. .ndo_change_mtu = ipoib_change_mtu,
  1076. .ndo_fix_features = ipoib_fix_features,
  1077. .ndo_start_xmit = ipoib_start_xmit,
  1078. .ndo_tx_timeout = ipoib_timeout,
  1079. .ndo_set_rx_mode = ipoib_set_mcast_list,
  1080. };
  1081. void ipoib_setup(struct net_device *dev)
  1082. {
  1083. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1084. dev->netdev_ops = &ipoib_netdev_ops;
  1085. dev->header_ops = &ipoib_header_ops;
  1086. ipoib_set_ethtool_ops(dev);
  1087. netif_napi_add(dev, &priv->napi, ipoib_poll, 100);
  1088. dev->watchdog_timeo = HZ;
  1089. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  1090. dev->hard_header_len = IPOIB_ENCAP_LEN;
  1091. dev->addr_len = INFINIBAND_ALEN;
  1092. dev->type = ARPHRD_INFINIBAND;
  1093. dev->tx_queue_len = ipoib_sendq_size * 2;
  1094. dev->features = (NETIF_F_VLAN_CHALLENGED |
  1095. NETIF_F_HIGHDMA);
  1096. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1097. memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
  1098. netif_carrier_off(dev);
  1099. priv->dev = dev;
  1100. spin_lock_init(&priv->lock);
  1101. mutex_init(&priv->vlan_mutex);
  1102. INIT_LIST_HEAD(&priv->path_list);
  1103. INIT_LIST_HEAD(&priv->child_intfs);
  1104. INIT_LIST_HEAD(&priv->dead_ahs);
  1105. INIT_LIST_HEAD(&priv->multicast_list);
  1106. INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
  1107. INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
  1108. INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
  1109. INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
  1110. INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
  1111. INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
  1112. INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
  1113. INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
  1114. INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
  1115. }
  1116. struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
  1117. {
  1118. struct net_device *dev;
  1119. dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
  1120. ipoib_setup);
  1121. if (!dev)
  1122. return NULL;
  1123. return netdev_priv(dev);
  1124. }
  1125. static ssize_t show_pkey(struct device *dev,
  1126. struct device_attribute *attr, char *buf)
  1127. {
  1128. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  1129. return sprintf(buf, "0x%04x\n", priv->pkey);
  1130. }
  1131. static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
  1132. static ssize_t show_umcast(struct device *dev,
  1133. struct device_attribute *attr, char *buf)
  1134. {
  1135. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  1136. return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
  1137. }
  1138. void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
  1139. {
  1140. struct ipoib_dev_priv *priv = netdev_priv(ndev);
  1141. if (umcast_val > 0) {
  1142. set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  1143. ipoib_warn(priv, "ignoring multicast groups joined directly "
  1144. "by userspace\n");
  1145. } else
  1146. clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  1147. }
  1148. static ssize_t set_umcast(struct device *dev,
  1149. struct device_attribute *attr,
  1150. const char *buf, size_t count)
  1151. {
  1152. unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
  1153. ipoib_set_umcast(to_net_dev(dev), umcast_val);
  1154. return count;
  1155. }
  1156. static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
  1157. int ipoib_add_umcast_attr(struct net_device *dev)
  1158. {
  1159. return device_create_file(&dev->dev, &dev_attr_umcast);
  1160. }
  1161. static ssize_t create_child(struct device *dev,
  1162. struct device_attribute *attr,
  1163. const char *buf, size_t count)
  1164. {
  1165. int pkey;
  1166. int ret;
  1167. if (sscanf(buf, "%i", &pkey) != 1)
  1168. return -EINVAL;
  1169. if (pkey < 0 || pkey > 0xffff)
  1170. return -EINVAL;
  1171. /*
  1172. * Set the full membership bit, so that we join the right
  1173. * broadcast group, etc.
  1174. */
  1175. pkey |= 0x8000;
  1176. ret = ipoib_vlan_add(to_net_dev(dev), pkey);
  1177. return ret ? ret : count;
  1178. }
  1179. static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
  1180. static ssize_t delete_child(struct device *dev,
  1181. struct device_attribute *attr,
  1182. const char *buf, size_t count)
  1183. {
  1184. int pkey;
  1185. int ret;
  1186. if (sscanf(buf, "%i", &pkey) != 1)
  1187. return -EINVAL;
  1188. if (pkey < 0 || pkey > 0xffff)
  1189. return -EINVAL;
  1190. ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
  1191. return ret ? ret : count;
  1192. }
  1193. static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
  1194. int ipoib_add_pkey_attr(struct net_device *dev)
  1195. {
  1196. return device_create_file(&dev->dev, &dev_attr_pkey);
  1197. }
  1198. int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
  1199. {
  1200. struct ib_device_attr *device_attr;
  1201. int result = -ENOMEM;
  1202. device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
  1203. if (!device_attr) {
  1204. printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
  1205. hca->name, sizeof *device_attr);
  1206. return result;
  1207. }
  1208. result = ib_query_device(hca, device_attr);
  1209. if (result) {
  1210. printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
  1211. hca->name, result);
  1212. kfree(device_attr);
  1213. return result;
  1214. }
  1215. priv->hca_caps = device_attr->device_cap_flags;
  1216. kfree(device_attr);
  1217. if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
  1218. priv->dev->hw_features = NETIF_F_SG |
  1219. NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  1220. if (priv->hca_caps & IB_DEVICE_UD_TSO)
  1221. priv->dev->hw_features |= NETIF_F_TSO;
  1222. priv->dev->features |= priv->dev->hw_features;
  1223. }
  1224. return 0;
  1225. }
  1226. static struct net_device *ipoib_add_port(const char *format,
  1227. struct ib_device *hca, u8 port)
  1228. {
  1229. struct ipoib_dev_priv *priv;
  1230. struct ib_port_attr attr;
  1231. int result = -ENOMEM;
  1232. priv = ipoib_intf_alloc(format);
  1233. if (!priv)
  1234. goto alloc_mem_failed;
  1235. SET_NETDEV_DEV(priv->dev, hca->dma_device);
  1236. priv->dev->dev_id = port - 1;
  1237. if (!ib_query_port(hca, port, &attr))
  1238. priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
  1239. else {
  1240. printk(KERN_WARNING "%s: ib_query_port %d failed\n",
  1241. hca->name, port);
  1242. goto device_init_failed;
  1243. }
  1244. /* MTU will be reset when mcast join happens */
  1245. priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
  1246. priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
  1247. priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
  1248. result = ib_query_pkey(hca, port, 0, &priv->pkey);
  1249. if (result) {
  1250. printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
  1251. hca->name, port, result);
  1252. goto device_init_failed;
  1253. }
  1254. if (ipoib_set_dev_features(priv, hca))
  1255. goto device_init_failed;
  1256. /*
  1257. * Set the full membership bit, so that we join the right
  1258. * broadcast group, etc.
  1259. */
  1260. priv->pkey |= 0x8000;
  1261. priv->dev->broadcast[8] = priv->pkey >> 8;
  1262. priv->dev->broadcast[9] = priv->pkey & 0xff;
  1263. result = ib_query_gid(hca, port, 0, &priv->local_gid);
  1264. if (result) {
  1265. printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
  1266. hca->name, port, result);
  1267. goto device_init_failed;
  1268. } else
  1269. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  1270. result = ipoib_dev_init(priv->dev, hca, port);
  1271. if (result < 0) {
  1272. printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
  1273. hca->name, port, result);
  1274. goto device_init_failed;
  1275. }
  1276. INIT_IB_EVENT_HANDLER(&priv->event_handler,
  1277. priv->ca, ipoib_event);
  1278. result = ib_register_event_handler(&priv->event_handler);
  1279. if (result < 0) {
  1280. printk(KERN_WARNING "%s: ib_register_event_handler failed for "
  1281. "port %d (ret = %d)\n",
  1282. hca->name, port, result);
  1283. goto event_failed;
  1284. }
  1285. result = register_netdev(priv->dev);
  1286. if (result) {
  1287. printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
  1288. hca->name, port, result);
  1289. goto register_failed;
  1290. }
  1291. ipoib_create_debug_files(priv->dev);
  1292. if (ipoib_cm_add_mode_attr(priv->dev))
  1293. goto sysfs_failed;
  1294. if (ipoib_add_pkey_attr(priv->dev))
  1295. goto sysfs_failed;
  1296. if (ipoib_add_umcast_attr(priv->dev))
  1297. goto sysfs_failed;
  1298. if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
  1299. goto sysfs_failed;
  1300. if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
  1301. goto sysfs_failed;
  1302. return priv->dev;
  1303. sysfs_failed:
  1304. ipoib_delete_debug_files(priv->dev);
  1305. unregister_netdev(priv->dev);
  1306. register_failed:
  1307. ib_unregister_event_handler(&priv->event_handler);
  1308. /* Stop GC if started before flush */
  1309. set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1310. cancel_delayed_work(&priv->neigh_reap_task);
  1311. flush_workqueue(ipoib_workqueue);
  1312. event_failed:
  1313. ipoib_dev_cleanup(priv->dev);
  1314. device_init_failed:
  1315. free_netdev(priv->dev);
  1316. alloc_mem_failed:
  1317. return ERR_PTR(result);
  1318. }
  1319. static void ipoib_add_one(struct ib_device *device)
  1320. {
  1321. struct list_head *dev_list;
  1322. struct net_device *dev;
  1323. struct ipoib_dev_priv *priv;
  1324. int s, e, p;
  1325. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  1326. return;
  1327. dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
  1328. if (!dev_list)
  1329. return;
  1330. INIT_LIST_HEAD(dev_list);
  1331. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  1332. s = 0;
  1333. e = 0;
  1334. } else {
  1335. s = 1;
  1336. e = device->phys_port_cnt;
  1337. }
  1338. for (p = s; p <= e; ++p) {
  1339. if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
  1340. continue;
  1341. dev = ipoib_add_port("ib%d", device, p);
  1342. if (!IS_ERR(dev)) {
  1343. priv = netdev_priv(dev);
  1344. list_add_tail(&priv->list, dev_list);
  1345. }
  1346. }
  1347. ib_set_client_data(device, &ipoib_client, dev_list);
  1348. }
  1349. static void ipoib_remove_one(struct ib_device *device)
  1350. {
  1351. struct ipoib_dev_priv *priv, *tmp;
  1352. struct list_head *dev_list;
  1353. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  1354. return;
  1355. dev_list = ib_get_client_data(device, &ipoib_client);
  1356. list_for_each_entry_safe(priv, tmp, dev_list, list) {
  1357. ib_unregister_event_handler(&priv->event_handler);
  1358. rtnl_lock();
  1359. dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
  1360. rtnl_unlock();
  1361. /* Stop GC */
  1362. set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1363. cancel_delayed_work(&priv->neigh_reap_task);
  1364. flush_workqueue(ipoib_workqueue);
  1365. unregister_netdev(priv->dev);
  1366. free_netdev(priv->dev);
  1367. }
  1368. kfree(dev_list);
  1369. }
  1370. static int __init ipoib_init_module(void)
  1371. {
  1372. int ret;
  1373. ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
  1374. ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
  1375. ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
  1376. ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
  1377. ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
  1378. ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
  1379. #ifdef CONFIG_INFINIBAND_IPOIB_CM
  1380. ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
  1381. #endif
  1382. /*
  1383. * When copying small received packets, we only copy from the
  1384. * linear data part of the SKB, so we rely on this condition.
  1385. */
  1386. BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
  1387. ret = ipoib_register_debugfs();
  1388. if (ret)
  1389. return ret;
  1390. /*
  1391. * We create our own workqueue mainly because we want to be
  1392. * able to flush it when devices are being removed. We can't
  1393. * use schedule_work()/flush_scheduled_work() because both
  1394. * unregister_netdev() and linkwatch_event take the rtnl lock,
  1395. * so flush_scheduled_work() can deadlock during device
  1396. * removal.
  1397. */
  1398. ipoib_workqueue = create_singlethread_workqueue("ipoib");
  1399. if (!ipoib_workqueue) {
  1400. ret = -ENOMEM;
  1401. goto err_fs;
  1402. }
  1403. ib_sa_register_client(&ipoib_sa_client);
  1404. ret = ib_register_client(&ipoib_client);
  1405. if (ret)
  1406. goto err_sa;
  1407. ret = ipoib_netlink_init();
  1408. if (ret)
  1409. goto err_client;
  1410. return 0;
  1411. err_client:
  1412. ib_unregister_client(&ipoib_client);
  1413. err_sa:
  1414. ib_sa_unregister_client(&ipoib_sa_client);
  1415. destroy_workqueue(ipoib_workqueue);
  1416. err_fs:
  1417. ipoib_unregister_debugfs();
  1418. return ret;
  1419. }
  1420. static void __exit ipoib_cleanup_module(void)
  1421. {
  1422. ipoib_netlink_fini();
  1423. ib_unregister_client(&ipoib_client);
  1424. ib_sa_unregister_client(&ipoib_sa_client);
  1425. ipoib_unregister_debugfs();
  1426. destroy_workqueue(ipoib_workqueue);
  1427. }
  1428. module_init(ipoib_init_module);
  1429. module_exit(ipoib_cleanup_module);