ipoib_main.c 45 KB

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