ipoib_main.c 44 KB

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