ipoib_main.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069
  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. #include <net/addrconf.h>
  46. #include <linux/inetdevice.h>
  47. #include <rdma/ib_cache.h>
  48. #define DRV_VERSION "1.0.0"
  49. const char ipoib_driver_version[] = DRV_VERSION;
  50. MODULE_AUTHOR("Roland Dreier");
  51. MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
  52. MODULE_LICENSE("Dual BSD/GPL");
  53. MODULE_VERSION(DRV_VERSION);
  54. int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
  55. int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
  56. module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
  57. MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
  58. module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
  59. MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
  60. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  61. int ipoib_debug_level;
  62. module_param_named(debug_level, ipoib_debug_level, int, 0644);
  63. MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
  64. #endif
  65. struct ipoib_path_iter {
  66. struct net_device *dev;
  67. struct ipoib_path path;
  68. };
  69. static const u8 ipv4_bcast_addr[] = {
  70. 0x00, 0xff, 0xff, 0xff,
  71. 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
  72. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
  73. };
  74. struct workqueue_struct *ipoib_workqueue;
  75. struct ib_sa_client ipoib_sa_client;
  76. static void ipoib_add_one(struct ib_device *device);
  77. static void ipoib_remove_one(struct ib_device *device, void *client_data);
  78. static void ipoib_neigh_reclaim(struct rcu_head *rp);
  79. static struct net_device *ipoib_get_net_dev_by_params(
  80. struct ib_device *dev, u8 port, u16 pkey,
  81. const union ib_gid *gid, const struct sockaddr *addr,
  82. void *client_data);
  83. static struct ib_client ipoib_client = {
  84. .name = "ipoib",
  85. .add = ipoib_add_one,
  86. .remove = ipoib_remove_one,
  87. .get_net_dev_by_params = ipoib_get_net_dev_by_params,
  88. };
  89. int ipoib_open(struct net_device *dev)
  90. {
  91. struct ipoib_dev_priv *priv = netdev_priv(dev);
  92. ipoib_dbg(priv, "bringing up interface\n");
  93. netif_carrier_off(dev);
  94. set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  95. if (ipoib_ib_dev_open(dev)) {
  96. if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
  97. return 0;
  98. goto err_disable;
  99. }
  100. if (ipoib_ib_dev_up(dev))
  101. goto err_stop;
  102. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  103. struct ipoib_dev_priv *cpriv;
  104. /* Bring up any child interfaces too */
  105. down_read(&priv->vlan_rwsem);
  106. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  107. int flags;
  108. flags = cpriv->dev->flags;
  109. if (flags & IFF_UP)
  110. continue;
  111. dev_change_flags(cpriv->dev, flags | IFF_UP);
  112. }
  113. up_read(&priv->vlan_rwsem);
  114. }
  115. netif_start_queue(dev);
  116. return 0;
  117. err_stop:
  118. ipoib_ib_dev_stop(dev);
  119. err_disable:
  120. clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  121. return -EINVAL;
  122. }
  123. static int ipoib_stop(struct net_device *dev)
  124. {
  125. struct ipoib_dev_priv *priv = netdev_priv(dev);
  126. ipoib_dbg(priv, "stopping interface\n");
  127. clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
  128. netif_stop_queue(dev);
  129. ipoib_ib_dev_down(dev);
  130. ipoib_ib_dev_stop(dev);
  131. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
  132. struct ipoib_dev_priv *cpriv;
  133. /* Bring down any child interfaces too */
  134. down_read(&priv->vlan_rwsem);
  135. list_for_each_entry(cpriv, &priv->child_intfs, list) {
  136. int flags;
  137. flags = cpriv->dev->flags;
  138. if (!(flags & IFF_UP))
  139. continue;
  140. dev_change_flags(cpriv->dev, flags & ~IFF_UP);
  141. }
  142. up_read(&priv->vlan_rwsem);
  143. }
  144. return 0;
  145. }
  146. static void ipoib_uninit(struct net_device *dev)
  147. {
  148. ipoib_dev_cleanup(dev);
  149. }
  150. static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
  151. {
  152. struct ipoib_dev_priv *priv = netdev_priv(dev);
  153. if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
  154. features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
  155. return features;
  156. }
  157. static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
  158. {
  159. struct ipoib_dev_priv *priv = netdev_priv(dev);
  160. /* dev->mtu > 2K ==> connected mode */
  161. if (ipoib_cm_admin_enabled(dev)) {
  162. if (new_mtu > ipoib_cm_max_mtu(dev))
  163. return -EINVAL;
  164. if (new_mtu > priv->mcast_mtu)
  165. ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
  166. priv->mcast_mtu);
  167. dev->mtu = new_mtu;
  168. return 0;
  169. }
  170. if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
  171. return -EINVAL;
  172. priv->admin_mtu = new_mtu;
  173. dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
  174. return 0;
  175. }
  176. /* Called with an RCU read lock taken */
  177. static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr,
  178. struct net_device *dev)
  179. {
  180. struct net *net = dev_net(dev);
  181. struct in_device *in_dev;
  182. struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
  183. struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
  184. __be32 ret_addr;
  185. switch (addr->sa_family) {
  186. case AF_INET:
  187. in_dev = in_dev_get(dev);
  188. if (!in_dev)
  189. return false;
  190. ret_addr = inet_confirm_addr(net, in_dev, 0,
  191. addr_in->sin_addr.s_addr,
  192. RT_SCOPE_HOST);
  193. in_dev_put(in_dev);
  194. if (ret_addr)
  195. return true;
  196. break;
  197. case AF_INET6:
  198. if (IS_ENABLED(CONFIG_IPV6) &&
  199. ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1))
  200. return true;
  201. break;
  202. }
  203. return false;
  204. }
  205. /**
  206. * Find the master net_device on top of the given net_device.
  207. * @dev: base IPoIB net_device
  208. *
  209. * Returns the master net_device with a reference held, or the same net_device
  210. * if no master exists.
  211. */
  212. static struct net_device *ipoib_get_master_net_dev(struct net_device *dev)
  213. {
  214. struct net_device *master;
  215. rcu_read_lock();
  216. master = netdev_master_upper_dev_get_rcu(dev);
  217. if (master)
  218. dev_hold(master);
  219. rcu_read_unlock();
  220. if (master)
  221. return master;
  222. dev_hold(dev);
  223. return dev;
  224. }
  225. /**
  226. * Find a net_device matching the given address, which is an upper device of
  227. * the given net_device.
  228. * @addr: IP address to look for.
  229. * @dev: base IPoIB net_device
  230. *
  231. * If found, returns the net_device with a reference held. Otherwise return
  232. * NULL.
  233. */
  234. static struct net_device *ipoib_get_net_dev_match_addr(
  235. const struct sockaddr *addr, struct net_device *dev)
  236. {
  237. struct net_device *upper,
  238. *result = NULL;
  239. struct list_head *iter;
  240. rcu_read_lock();
  241. if (ipoib_is_dev_match_addr_rcu(addr, dev)) {
  242. dev_hold(dev);
  243. result = dev;
  244. goto out;
  245. }
  246. netdev_for_each_all_upper_dev_rcu(dev, upper, iter) {
  247. if (ipoib_is_dev_match_addr_rcu(addr, upper)) {
  248. dev_hold(upper);
  249. result = upper;
  250. break;
  251. }
  252. }
  253. out:
  254. rcu_read_unlock();
  255. return result;
  256. }
  257. /* returns the number of IPoIB netdevs on top a given ipoib device matching a
  258. * pkey_index and address, if one exists.
  259. *
  260. * @found_net_dev: contains a matching net_device if the return value >= 1,
  261. * with a reference held. */
  262. static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
  263. const union ib_gid *gid,
  264. u16 pkey_index,
  265. const struct sockaddr *addr,
  266. int nesting,
  267. struct net_device **found_net_dev)
  268. {
  269. struct ipoib_dev_priv *child_priv;
  270. struct net_device *net_dev = NULL;
  271. int matches = 0;
  272. if (priv->pkey_index == pkey_index &&
  273. (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
  274. if (!addr) {
  275. net_dev = ipoib_get_master_net_dev(priv->dev);
  276. } else {
  277. /* Verify the net_device matches the IP address, as
  278. * IPoIB child devices currently share a GID. */
  279. net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev);
  280. }
  281. if (net_dev) {
  282. if (!*found_net_dev)
  283. *found_net_dev = net_dev;
  284. else
  285. dev_put(net_dev);
  286. ++matches;
  287. }
  288. }
  289. /* Check child interfaces */
  290. down_read_nested(&priv->vlan_rwsem, nesting);
  291. list_for_each_entry(child_priv, &priv->child_intfs, list) {
  292. matches += ipoib_match_gid_pkey_addr(child_priv, gid,
  293. pkey_index, addr,
  294. nesting + 1,
  295. found_net_dev);
  296. if (matches > 1)
  297. break;
  298. }
  299. up_read(&priv->vlan_rwsem);
  300. return matches;
  301. }
  302. /* Returns the number of matching net_devs found (between 0 and 2). Also
  303. * return the matching net_device in the @net_dev parameter, holding a
  304. * reference to the net_device, if the number of matches >= 1 */
  305. static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
  306. u16 pkey_index,
  307. const union ib_gid *gid,
  308. const struct sockaddr *addr,
  309. struct net_device **net_dev)
  310. {
  311. struct ipoib_dev_priv *priv;
  312. int matches = 0;
  313. *net_dev = NULL;
  314. list_for_each_entry(priv, dev_list, list) {
  315. if (priv->port != port)
  316. continue;
  317. matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
  318. addr, 0, net_dev);
  319. if (matches > 1)
  320. break;
  321. }
  322. return matches;
  323. }
  324. static struct net_device *ipoib_get_net_dev_by_params(
  325. struct ib_device *dev, u8 port, u16 pkey,
  326. const union ib_gid *gid, const struct sockaddr *addr,
  327. void *client_data)
  328. {
  329. struct net_device *net_dev;
  330. struct list_head *dev_list = client_data;
  331. u16 pkey_index;
  332. int matches;
  333. int ret;
  334. if (!rdma_protocol_ib(dev, port))
  335. return NULL;
  336. ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
  337. if (ret)
  338. return NULL;
  339. if (!dev_list)
  340. return NULL;
  341. /* See if we can find a unique device matching the L2 parameters */
  342. matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
  343. gid, NULL, &net_dev);
  344. switch (matches) {
  345. case 0:
  346. return NULL;
  347. case 1:
  348. return net_dev;
  349. }
  350. dev_put(net_dev);
  351. /* Couldn't find a unique device with L2 parameters only. Use L3
  352. * address to uniquely match the net device */
  353. matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
  354. gid, addr, &net_dev);
  355. switch (matches) {
  356. case 0:
  357. return NULL;
  358. default:
  359. dev_warn_ratelimited(&dev->dev,
  360. "duplicate IP address detected\n");
  361. /* Fall through */
  362. case 1:
  363. return net_dev;
  364. }
  365. }
  366. int ipoib_set_mode(struct net_device *dev, const char *buf)
  367. {
  368. struct ipoib_dev_priv *priv = netdev_priv(dev);
  369. /* flush paths if we switch modes so that connections are restarted */
  370. if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
  371. set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
  372. ipoib_warn(priv, "enabling connected mode "
  373. "will cause multicast packet drops\n");
  374. netdev_update_features(dev);
  375. dev_set_mtu(dev, ipoib_cm_max_mtu(dev));
  376. rtnl_unlock();
  377. priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
  378. ipoib_flush_paths(dev);
  379. rtnl_lock();
  380. return 0;
  381. }
  382. if (!strcmp(buf, "datagram\n")) {
  383. clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
  384. netdev_update_features(dev);
  385. dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
  386. rtnl_unlock();
  387. ipoib_flush_paths(dev);
  388. rtnl_lock();
  389. return 0;
  390. }
  391. return -EINVAL;
  392. }
  393. static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
  394. {
  395. struct ipoib_dev_priv *priv = netdev_priv(dev);
  396. struct rb_node *n = priv->path_tree.rb_node;
  397. struct ipoib_path *path;
  398. int ret;
  399. while (n) {
  400. path = rb_entry(n, struct ipoib_path, rb_node);
  401. ret = memcmp(gid, path->pathrec.dgid.raw,
  402. sizeof (union ib_gid));
  403. if (ret < 0)
  404. n = n->rb_left;
  405. else if (ret > 0)
  406. n = n->rb_right;
  407. else
  408. return path;
  409. }
  410. return NULL;
  411. }
  412. static int __path_add(struct net_device *dev, struct ipoib_path *path)
  413. {
  414. struct ipoib_dev_priv *priv = netdev_priv(dev);
  415. struct rb_node **n = &priv->path_tree.rb_node;
  416. struct rb_node *pn = NULL;
  417. struct ipoib_path *tpath;
  418. int ret;
  419. while (*n) {
  420. pn = *n;
  421. tpath = rb_entry(pn, struct ipoib_path, rb_node);
  422. ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
  423. sizeof (union ib_gid));
  424. if (ret < 0)
  425. n = &pn->rb_left;
  426. else if (ret > 0)
  427. n = &pn->rb_right;
  428. else
  429. return -EEXIST;
  430. }
  431. rb_link_node(&path->rb_node, pn, n);
  432. rb_insert_color(&path->rb_node, &priv->path_tree);
  433. list_add_tail(&path->list, &priv->path_list);
  434. return 0;
  435. }
  436. static void path_free(struct net_device *dev, struct ipoib_path *path)
  437. {
  438. struct sk_buff *skb;
  439. while ((skb = __skb_dequeue(&path->queue)))
  440. dev_kfree_skb_irq(skb);
  441. ipoib_dbg(netdev_priv(dev), "path_free\n");
  442. /* remove all neigh connected to this path */
  443. ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
  444. if (path->ah)
  445. ipoib_put_ah(path->ah);
  446. kfree(path);
  447. }
  448. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  449. struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
  450. {
  451. struct ipoib_path_iter *iter;
  452. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  453. if (!iter)
  454. return NULL;
  455. iter->dev = dev;
  456. memset(iter->path.pathrec.dgid.raw, 0, 16);
  457. if (ipoib_path_iter_next(iter)) {
  458. kfree(iter);
  459. return NULL;
  460. }
  461. return iter;
  462. }
  463. int ipoib_path_iter_next(struct ipoib_path_iter *iter)
  464. {
  465. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  466. struct rb_node *n;
  467. struct ipoib_path *path;
  468. int ret = 1;
  469. spin_lock_irq(&priv->lock);
  470. n = rb_first(&priv->path_tree);
  471. while (n) {
  472. path = rb_entry(n, struct ipoib_path, rb_node);
  473. if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
  474. sizeof (union ib_gid)) < 0) {
  475. iter->path = *path;
  476. ret = 0;
  477. break;
  478. }
  479. n = rb_next(n);
  480. }
  481. spin_unlock_irq(&priv->lock);
  482. return ret;
  483. }
  484. void ipoib_path_iter_read(struct ipoib_path_iter *iter,
  485. struct ipoib_path *path)
  486. {
  487. *path = iter->path;
  488. }
  489. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
  490. void ipoib_mark_paths_invalid(struct net_device *dev)
  491. {
  492. struct ipoib_dev_priv *priv = netdev_priv(dev);
  493. struct ipoib_path *path, *tp;
  494. spin_lock_irq(&priv->lock);
  495. list_for_each_entry_safe(path, tp, &priv->path_list, list) {
  496. ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
  497. be16_to_cpu(path->pathrec.dlid),
  498. path->pathrec.dgid.raw);
  499. path->valid = 0;
  500. }
  501. spin_unlock_irq(&priv->lock);
  502. }
  503. void ipoib_flush_paths(struct net_device *dev)
  504. {
  505. struct ipoib_dev_priv *priv = netdev_priv(dev);
  506. struct ipoib_path *path, *tp;
  507. LIST_HEAD(remove_list);
  508. unsigned long flags;
  509. netif_tx_lock_bh(dev);
  510. spin_lock_irqsave(&priv->lock, flags);
  511. list_splice_init(&priv->path_list, &remove_list);
  512. list_for_each_entry(path, &remove_list, list)
  513. rb_erase(&path->rb_node, &priv->path_tree);
  514. list_for_each_entry_safe(path, tp, &remove_list, list) {
  515. if (path->query)
  516. ib_sa_cancel_query(path->query_id, path->query);
  517. spin_unlock_irqrestore(&priv->lock, flags);
  518. netif_tx_unlock_bh(dev);
  519. wait_for_completion(&path->done);
  520. path_free(dev, path);
  521. netif_tx_lock_bh(dev);
  522. spin_lock_irqsave(&priv->lock, flags);
  523. }
  524. spin_unlock_irqrestore(&priv->lock, flags);
  525. netif_tx_unlock_bh(dev);
  526. }
  527. static void path_rec_completion(int status,
  528. struct ib_sa_path_rec *pathrec,
  529. void *path_ptr)
  530. {
  531. struct ipoib_path *path = path_ptr;
  532. struct net_device *dev = path->dev;
  533. struct ipoib_dev_priv *priv = netdev_priv(dev);
  534. struct ipoib_ah *ah = NULL;
  535. struct ipoib_ah *old_ah = NULL;
  536. struct ipoib_neigh *neigh, *tn;
  537. struct sk_buff_head skqueue;
  538. struct sk_buff *skb;
  539. unsigned long flags;
  540. if (!status)
  541. ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
  542. be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
  543. else
  544. ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
  545. status, path->pathrec.dgid.raw);
  546. skb_queue_head_init(&skqueue);
  547. if (!status) {
  548. struct ib_ah_attr av;
  549. if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
  550. ah = ipoib_create_ah(dev, priv->pd, &av);
  551. }
  552. spin_lock_irqsave(&priv->lock, flags);
  553. if (!IS_ERR_OR_NULL(ah)) {
  554. path->pathrec = *pathrec;
  555. old_ah = path->ah;
  556. path->ah = ah;
  557. ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
  558. ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
  559. while ((skb = __skb_dequeue(&path->queue)))
  560. __skb_queue_tail(&skqueue, skb);
  561. list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
  562. if (neigh->ah) {
  563. WARN_ON(neigh->ah != old_ah);
  564. /*
  565. * Dropping the ah reference inside
  566. * priv->lock is safe here, because we
  567. * will hold one more reference from
  568. * the original value of path->ah (ie
  569. * old_ah).
  570. */
  571. ipoib_put_ah(neigh->ah);
  572. }
  573. kref_get(&path->ah->ref);
  574. neigh->ah = path->ah;
  575. if (ipoib_cm_enabled(dev, neigh->daddr)) {
  576. if (!ipoib_cm_get(neigh))
  577. ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
  578. path,
  579. neigh));
  580. if (!ipoib_cm_get(neigh)) {
  581. ipoib_neigh_free(neigh);
  582. continue;
  583. }
  584. }
  585. while ((skb = __skb_dequeue(&neigh->queue)))
  586. __skb_queue_tail(&skqueue, skb);
  587. }
  588. path->valid = 1;
  589. }
  590. path->query = NULL;
  591. complete(&path->done);
  592. spin_unlock_irqrestore(&priv->lock, flags);
  593. if (IS_ERR_OR_NULL(ah))
  594. ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
  595. if (old_ah)
  596. ipoib_put_ah(old_ah);
  597. while ((skb = __skb_dequeue(&skqueue))) {
  598. skb->dev = dev;
  599. if (dev_queue_xmit(skb))
  600. ipoib_warn(priv, "dev_queue_xmit failed "
  601. "to requeue packet\n");
  602. }
  603. }
  604. static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
  605. {
  606. struct ipoib_dev_priv *priv = netdev_priv(dev);
  607. struct ipoib_path *path;
  608. if (!priv->broadcast)
  609. return NULL;
  610. path = kzalloc(sizeof *path, GFP_ATOMIC);
  611. if (!path)
  612. return NULL;
  613. path->dev = dev;
  614. skb_queue_head_init(&path->queue);
  615. INIT_LIST_HEAD(&path->neigh_list);
  616. memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
  617. path->pathrec.sgid = priv->local_gid;
  618. path->pathrec.pkey = cpu_to_be16(priv->pkey);
  619. path->pathrec.numb_path = 1;
  620. path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
  621. return path;
  622. }
  623. static int path_rec_start(struct net_device *dev,
  624. struct ipoib_path *path)
  625. {
  626. struct ipoib_dev_priv *priv = netdev_priv(dev);
  627. ipoib_dbg(priv, "Start path record lookup for %pI6\n",
  628. path->pathrec.dgid.raw);
  629. init_completion(&path->done);
  630. path->query_id =
  631. ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
  632. &path->pathrec,
  633. IB_SA_PATH_REC_DGID |
  634. IB_SA_PATH_REC_SGID |
  635. IB_SA_PATH_REC_NUMB_PATH |
  636. IB_SA_PATH_REC_TRAFFIC_CLASS |
  637. IB_SA_PATH_REC_PKEY,
  638. 1000, GFP_ATOMIC,
  639. path_rec_completion,
  640. path, &path->query);
  641. if (path->query_id < 0) {
  642. ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
  643. path->query = NULL;
  644. complete(&path->done);
  645. return path->query_id;
  646. }
  647. return 0;
  648. }
  649. static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
  650. struct net_device *dev)
  651. {
  652. struct ipoib_dev_priv *priv = netdev_priv(dev);
  653. struct ipoib_path *path;
  654. struct ipoib_neigh *neigh;
  655. unsigned long flags;
  656. spin_lock_irqsave(&priv->lock, flags);
  657. neigh = ipoib_neigh_alloc(daddr, dev);
  658. if (!neigh) {
  659. spin_unlock_irqrestore(&priv->lock, flags);
  660. ++dev->stats.tx_dropped;
  661. dev_kfree_skb_any(skb);
  662. return;
  663. }
  664. path = __path_find(dev, daddr + 4);
  665. if (!path) {
  666. path = path_rec_create(dev, daddr + 4);
  667. if (!path)
  668. goto err_path;
  669. __path_add(dev, path);
  670. }
  671. list_add_tail(&neigh->list, &path->neigh_list);
  672. if (path->ah) {
  673. kref_get(&path->ah->ref);
  674. neigh->ah = path->ah;
  675. if (ipoib_cm_enabled(dev, neigh->daddr)) {
  676. if (!ipoib_cm_get(neigh))
  677. ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
  678. if (!ipoib_cm_get(neigh)) {
  679. ipoib_neigh_free(neigh);
  680. goto err_drop;
  681. }
  682. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
  683. __skb_queue_tail(&neigh->queue, skb);
  684. else {
  685. ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
  686. skb_queue_len(&neigh->queue));
  687. goto err_drop;
  688. }
  689. } else {
  690. spin_unlock_irqrestore(&priv->lock, flags);
  691. ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
  692. ipoib_neigh_put(neigh);
  693. return;
  694. }
  695. } else {
  696. neigh->ah = NULL;
  697. if (!path->query && path_rec_start(dev, path))
  698. goto err_path;
  699. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
  700. __skb_queue_tail(&neigh->queue, skb);
  701. else
  702. goto err_drop;
  703. }
  704. spin_unlock_irqrestore(&priv->lock, flags);
  705. ipoib_neigh_put(neigh);
  706. return;
  707. err_path:
  708. ipoib_neigh_free(neigh);
  709. err_drop:
  710. ++dev->stats.tx_dropped;
  711. dev_kfree_skb_any(skb);
  712. spin_unlock_irqrestore(&priv->lock, flags);
  713. ipoib_neigh_put(neigh);
  714. }
  715. static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
  716. struct ipoib_cb *cb)
  717. {
  718. struct ipoib_dev_priv *priv = netdev_priv(dev);
  719. struct ipoib_path *path;
  720. unsigned long flags;
  721. spin_lock_irqsave(&priv->lock, flags);
  722. path = __path_find(dev, cb->hwaddr + 4);
  723. if (!path || !path->valid) {
  724. int new_path = 0;
  725. if (!path) {
  726. path = path_rec_create(dev, cb->hwaddr + 4);
  727. new_path = 1;
  728. }
  729. if (path) {
  730. if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  731. __skb_queue_tail(&path->queue, skb);
  732. } else {
  733. ++dev->stats.tx_dropped;
  734. dev_kfree_skb_any(skb);
  735. }
  736. if (!path->query && path_rec_start(dev, path)) {
  737. spin_unlock_irqrestore(&priv->lock, flags);
  738. if (new_path)
  739. path_free(dev, path);
  740. return;
  741. } else
  742. __path_add(dev, path);
  743. } else {
  744. ++dev->stats.tx_dropped;
  745. dev_kfree_skb_any(skb);
  746. }
  747. spin_unlock_irqrestore(&priv->lock, flags);
  748. return;
  749. }
  750. if (path->ah) {
  751. ipoib_dbg(priv, "Send unicast ARP to %04x\n",
  752. be16_to_cpu(path->pathrec.dlid));
  753. spin_unlock_irqrestore(&priv->lock, flags);
  754. ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
  755. return;
  756. } else if ((path->query || !path_rec_start(dev, path)) &&
  757. skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  758. __skb_queue_tail(&path->queue, skb);
  759. } else {
  760. ++dev->stats.tx_dropped;
  761. dev_kfree_skb_any(skb);
  762. }
  763. spin_unlock_irqrestore(&priv->lock, flags);
  764. }
  765. static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
  766. {
  767. struct ipoib_dev_priv *priv = netdev_priv(dev);
  768. struct ipoib_neigh *neigh;
  769. struct ipoib_cb *cb = ipoib_skb_cb(skb);
  770. struct ipoib_header *header;
  771. unsigned long flags;
  772. header = (struct ipoib_header *) skb->data;
  773. if (unlikely(cb->hwaddr[4] == 0xff)) {
  774. /* multicast, arrange "if" according to probability */
  775. if ((header->proto != htons(ETH_P_IP)) &&
  776. (header->proto != htons(ETH_P_IPV6)) &&
  777. (header->proto != htons(ETH_P_ARP)) &&
  778. (header->proto != htons(ETH_P_RARP)) &&
  779. (header->proto != htons(ETH_P_TIPC))) {
  780. /* ethertype not supported by IPoIB */
  781. ++dev->stats.tx_dropped;
  782. dev_kfree_skb_any(skb);
  783. return NETDEV_TX_OK;
  784. }
  785. /* Add in the P_Key for multicast*/
  786. cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
  787. cb->hwaddr[9] = priv->pkey & 0xff;
  788. neigh = ipoib_neigh_get(dev, cb->hwaddr);
  789. if (likely(neigh))
  790. goto send_using_neigh;
  791. ipoib_mcast_send(dev, cb->hwaddr, skb);
  792. return NETDEV_TX_OK;
  793. }
  794. /* unicast, arrange "switch" according to probability */
  795. switch (header->proto) {
  796. case htons(ETH_P_IP):
  797. case htons(ETH_P_IPV6):
  798. case htons(ETH_P_TIPC):
  799. neigh = ipoib_neigh_get(dev, cb->hwaddr);
  800. if (unlikely(!neigh)) {
  801. neigh_add_path(skb, cb->hwaddr, dev);
  802. return NETDEV_TX_OK;
  803. }
  804. break;
  805. case htons(ETH_P_ARP):
  806. case htons(ETH_P_RARP):
  807. /* for unicast ARP and RARP should always perform path find */
  808. unicast_arp_send(skb, dev, cb);
  809. return NETDEV_TX_OK;
  810. default:
  811. /* ethertype not supported by IPoIB */
  812. ++dev->stats.tx_dropped;
  813. dev_kfree_skb_any(skb);
  814. return NETDEV_TX_OK;
  815. }
  816. send_using_neigh:
  817. /* note we now hold a ref to neigh */
  818. if (ipoib_cm_get(neigh)) {
  819. if (ipoib_cm_up(neigh)) {
  820. ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
  821. goto unref;
  822. }
  823. } else if (neigh->ah) {
  824. ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
  825. goto unref;
  826. }
  827. if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
  828. spin_lock_irqsave(&priv->lock, flags);
  829. __skb_queue_tail(&neigh->queue, skb);
  830. spin_unlock_irqrestore(&priv->lock, flags);
  831. } else {
  832. ++dev->stats.tx_dropped;
  833. dev_kfree_skb_any(skb);
  834. }
  835. unref:
  836. ipoib_neigh_put(neigh);
  837. return NETDEV_TX_OK;
  838. }
  839. static void ipoib_timeout(struct net_device *dev)
  840. {
  841. struct ipoib_dev_priv *priv = netdev_priv(dev);
  842. ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
  843. jiffies_to_msecs(jiffies - dev->trans_start));
  844. ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
  845. netif_queue_stopped(dev),
  846. priv->tx_head, priv->tx_tail);
  847. /* XXX reset QP, etc. */
  848. }
  849. static int ipoib_hard_header(struct sk_buff *skb,
  850. struct net_device *dev,
  851. unsigned short type,
  852. const void *daddr, const void *saddr, unsigned len)
  853. {
  854. struct ipoib_header *header;
  855. struct ipoib_cb *cb = ipoib_skb_cb(skb);
  856. header = (struct ipoib_header *) skb_push(skb, sizeof *header);
  857. header->proto = htons(type);
  858. header->reserved = 0;
  859. /*
  860. * we don't rely on dst_entry structure, always stuff the
  861. * destination address into skb->cb so we can figure out where
  862. * to send the packet later.
  863. */
  864. memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
  865. return sizeof *header;
  866. }
  867. static void ipoib_set_mcast_list(struct net_device *dev)
  868. {
  869. struct ipoib_dev_priv *priv = netdev_priv(dev);
  870. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  871. ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
  872. return;
  873. }
  874. queue_work(priv->wq, &priv->restart_task);
  875. }
  876. static int ipoib_get_iflink(const struct net_device *dev)
  877. {
  878. struct ipoib_dev_priv *priv = netdev_priv(dev);
  879. /* parent interface */
  880. if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
  881. return dev->ifindex;
  882. /* child/vlan interface */
  883. return priv->parent->ifindex;
  884. }
  885. static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
  886. {
  887. /*
  888. * Use only the address parts that contributes to spreading
  889. * The subnet prefix is not used as one can not connect to
  890. * same remote port (GUID) using the same remote QPN via two
  891. * different subnets.
  892. */
  893. /* qpn octets[1:4) & port GUID octets[12:20) */
  894. u32 *d32 = (u32 *) daddr;
  895. u32 hv;
  896. hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
  897. return hv & htbl->mask;
  898. }
  899. struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
  900. {
  901. struct ipoib_dev_priv *priv = netdev_priv(dev);
  902. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  903. struct ipoib_neigh_hash *htbl;
  904. struct ipoib_neigh *neigh = NULL;
  905. u32 hash_val;
  906. rcu_read_lock_bh();
  907. htbl = rcu_dereference_bh(ntbl->htbl);
  908. if (!htbl)
  909. goto out_unlock;
  910. hash_val = ipoib_addr_hash(htbl, daddr);
  911. for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
  912. neigh != NULL;
  913. neigh = rcu_dereference_bh(neigh->hnext)) {
  914. if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
  915. /* found, take one ref on behalf of the caller */
  916. if (!atomic_inc_not_zero(&neigh->refcnt)) {
  917. /* deleted */
  918. neigh = NULL;
  919. goto out_unlock;
  920. }
  921. neigh->alive = jiffies;
  922. goto out_unlock;
  923. }
  924. }
  925. out_unlock:
  926. rcu_read_unlock_bh();
  927. return neigh;
  928. }
  929. static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
  930. {
  931. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  932. struct ipoib_neigh_hash *htbl;
  933. unsigned long neigh_obsolete;
  934. unsigned long dt;
  935. unsigned long flags;
  936. int i;
  937. LIST_HEAD(remove_list);
  938. struct ipoib_mcast *mcast, *tmcast;
  939. struct net_device *dev = priv->dev;
  940. if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  941. return;
  942. spin_lock_irqsave(&priv->lock, flags);
  943. htbl = rcu_dereference_protected(ntbl->htbl,
  944. lockdep_is_held(&priv->lock));
  945. if (!htbl)
  946. goto out_unlock;
  947. /* neigh is obsolete if it was idle for two GC periods */
  948. dt = 2 * arp_tbl.gc_interval;
  949. neigh_obsolete = jiffies - dt;
  950. /* handle possible race condition */
  951. if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  952. goto out_unlock;
  953. for (i = 0; i < htbl->size; i++) {
  954. struct ipoib_neigh *neigh;
  955. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  956. while ((neigh = rcu_dereference_protected(*np,
  957. lockdep_is_held(&priv->lock))) != NULL) {
  958. /* was the neigh idle for two GC periods */
  959. if (time_after(neigh_obsolete, neigh->alive)) {
  960. u8 *mgid = neigh->daddr + 4;
  961. /* Is this multicast ? */
  962. if (*mgid == 0xff) {
  963. mcast = __ipoib_mcast_find(dev, mgid);
  964. if (mcast && test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  965. list_del(&mcast->list);
  966. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  967. list_add_tail(&mcast->list, &remove_list);
  968. }
  969. }
  970. rcu_assign_pointer(*np,
  971. rcu_dereference_protected(neigh->hnext,
  972. lockdep_is_held(&priv->lock)));
  973. /* remove from path/mc list */
  974. list_del(&neigh->list);
  975. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  976. } else {
  977. np = &neigh->hnext;
  978. }
  979. }
  980. }
  981. out_unlock:
  982. spin_unlock_irqrestore(&priv->lock, flags);
  983. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  984. ipoib_mcast_leave(dev, mcast);
  985. ipoib_mcast_free(mcast);
  986. }
  987. }
  988. static void ipoib_reap_neigh(struct work_struct *work)
  989. {
  990. struct ipoib_dev_priv *priv =
  991. container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
  992. __ipoib_reap_neigh(priv);
  993. if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
  994. queue_delayed_work(priv->wq, &priv->neigh_reap_task,
  995. arp_tbl.gc_interval);
  996. }
  997. static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
  998. struct net_device *dev)
  999. {
  1000. struct ipoib_neigh *neigh;
  1001. neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
  1002. if (!neigh)
  1003. return NULL;
  1004. neigh->dev = dev;
  1005. memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
  1006. skb_queue_head_init(&neigh->queue);
  1007. INIT_LIST_HEAD(&neigh->list);
  1008. ipoib_cm_set(neigh, NULL);
  1009. /* one ref on behalf of the caller */
  1010. atomic_set(&neigh->refcnt, 1);
  1011. return neigh;
  1012. }
  1013. struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
  1014. struct net_device *dev)
  1015. {
  1016. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1017. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  1018. struct ipoib_neigh_hash *htbl;
  1019. struct ipoib_neigh *neigh;
  1020. u32 hash_val;
  1021. htbl = rcu_dereference_protected(ntbl->htbl,
  1022. lockdep_is_held(&priv->lock));
  1023. if (!htbl) {
  1024. neigh = NULL;
  1025. goto out_unlock;
  1026. }
  1027. /* need to add a new neigh, but maybe some other thread succeeded?
  1028. * recalc hash, maybe hash resize took place so we do a search
  1029. */
  1030. hash_val = ipoib_addr_hash(htbl, daddr);
  1031. for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
  1032. lockdep_is_held(&priv->lock));
  1033. neigh != NULL;
  1034. neigh = rcu_dereference_protected(neigh->hnext,
  1035. lockdep_is_held(&priv->lock))) {
  1036. if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
  1037. /* found, take one ref on behalf of the caller */
  1038. if (!atomic_inc_not_zero(&neigh->refcnt)) {
  1039. /* deleted */
  1040. neigh = NULL;
  1041. break;
  1042. }
  1043. neigh->alive = jiffies;
  1044. goto out_unlock;
  1045. }
  1046. }
  1047. neigh = ipoib_neigh_ctor(daddr, dev);
  1048. if (!neigh)
  1049. goto out_unlock;
  1050. /* one ref on behalf of the hash table */
  1051. atomic_inc(&neigh->refcnt);
  1052. neigh->alive = jiffies;
  1053. /* put in hash */
  1054. rcu_assign_pointer(neigh->hnext,
  1055. rcu_dereference_protected(htbl->buckets[hash_val],
  1056. lockdep_is_held(&priv->lock)));
  1057. rcu_assign_pointer(htbl->buckets[hash_val], neigh);
  1058. atomic_inc(&ntbl->entries);
  1059. out_unlock:
  1060. return neigh;
  1061. }
  1062. void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
  1063. {
  1064. /* neigh reference count was dropprd to zero */
  1065. struct net_device *dev = neigh->dev;
  1066. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1067. struct sk_buff *skb;
  1068. if (neigh->ah)
  1069. ipoib_put_ah(neigh->ah);
  1070. while ((skb = __skb_dequeue(&neigh->queue))) {
  1071. ++dev->stats.tx_dropped;
  1072. dev_kfree_skb_any(skb);
  1073. }
  1074. if (ipoib_cm_get(neigh))
  1075. ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
  1076. ipoib_dbg(netdev_priv(dev),
  1077. "neigh free for %06x %pI6\n",
  1078. IPOIB_QPN(neigh->daddr),
  1079. neigh->daddr + 4);
  1080. kfree(neigh);
  1081. if (atomic_dec_and_test(&priv->ntbl.entries)) {
  1082. if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
  1083. complete(&priv->ntbl.flushed);
  1084. }
  1085. }
  1086. static void ipoib_neigh_reclaim(struct rcu_head *rp)
  1087. {
  1088. /* Called as a result of removal from hash table */
  1089. struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
  1090. /* note TX context may hold another ref */
  1091. ipoib_neigh_put(neigh);
  1092. }
  1093. void ipoib_neigh_free(struct ipoib_neigh *neigh)
  1094. {
  1095. struct net_device *dev = neigh->dev;
  1096. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1097. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  1098. struct ipoib_neigh_hash *htbl;
  1099. struct ipoib_neigh __rcu **np;
  1100. struct ipoib_neigh *n;
  1101. u32 hash_val;
  1102. htbl = rcu_dereference_protected(ntbl->htbl,
  1103. lockdep_is_held(&priv->lock));
  1104. if (!htbl)
  1105. return;
  1106. hash_val = ipoib_addr_hash(htbl, neigh->daddr);
  1107. np = &htbl->buckets[hash_val];
  1108. for (n = rcu_dereference_protected(*np,
  1109. lockdep_is_held(&priv->lock));
  1110. n != NULL;
  1111. n = rcu_dereference_protected(*np,
  1112. lockdep_is_held(&priv->lock))) {
  1113. if (n == neigh) {
  1114. /* found */
  1115. rcu_assign_pointer(*np,
  1116. rcu_dereference_protected(neigh->hnext,
  1117. lockdep_is_held(&priv->lock)));
  1118. /* remove from parent list */
  1119. list_del(&neigh->list);
  1120. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  1121. return;
  1122. } else {
  1123. np = &n->hnext;
  1124. }
  1125. }
  1126. }
  1127. static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
  1128. {
  1129. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  1130. struct ipoib_neigh_hash *htbl;
  1131. struct ipoib_neigh __rcu **buckets;
  1132. u32 size;
  1133. clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
  1134. ntbl->htbl = NULL;
  1135. htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
  1136. if (!htbl)
  1137. return -ENOMEM;
  1138. set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1139. size = roundup_pow_of_two(arp_tbl.gc_thresh3);
  1140. buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
  1141. if (!buckets) {
  1142. kfree(htbl);
  1143. return -ENOMEM;
  1144. }
  1145. htbl->size = size;
  1146. htbl->mask = (size - 1);
  1147. htbl->buckets = buckets;
  1148. RCU_INIT_POINTER(ntbl->htbl, htbl);
  1149. htbl->ntbl = ntbl;
  1150. atomic_set(&ntbl->entries, 0);
  1151. /* start garbage collection */
  1152. clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1153. queue_delayed_work(priv->wq, &priv->neigh_reap_task,
  1154. arp_tbl.gc_interval);
  1155. return 0;
  1156. }
  1157. static void neigh_hash_free_rcu(struct rcu_head *head)
  1158. {
  1159. struct ipoib_neigh_hash *htbl = container_of(head,
  1160. struct ipoib_neigh_hash,
  1161. rcu);
  1162. struct ipoib_neigh __rcu **buckets = htbl->buckets;
  1163. struct ipoib_neigh_table *ntbl = htbl->ntbl;
  1164. kfree(buckets);
  1165. kfree(htbl);
  1166. complete(&ntbl->deleted);
  1167. }
  1168. void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
  1169. {
  1170. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1171. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  1172. struct ipoib_neigh_hash *htbl;
  1173. unsigned long flags;
  1174. int i;
  1175. /* remove all neigh connected to a given path or mcast */
  1176. spin_lock_irqsave(&priv->lock, flags);
  1177. htbl = rcu_dereference_protected(ntbl->htbl,
  1178. lockdep_is_held(&priv->lock));
  1179. if (!htbl)
  1180. goto out_unlock;
  1181. for (i = 0; i < htbl->size; i++) {
  1182. struct ipoib_neigh *neigh;
  1183. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  1184. while ((neigh = rcu_dereference_protected(*np,
  1185. lockdep_is_held(&priv->lock))) != NULL) {
  1186. /* delete neighs belong to this parent */
  1187. if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
  1188. rcu_assign_pointer(*np,
  1189. rcu_dereference_protected(neigh->hnext,
  1190. lockdep_is_held(&priv->lock)));
  1191. /* remove from parent list */
  1192. list_del(&neigh->list);
  1193. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  1194. } else {
  1195. np = &neigh->hnext;
  1196. }
  1197. }
  1198. }
  1199. out_unlock:
  1200. spin_unlock_irqrestore(&priv->lock, flags);
  1201. }
  1202. static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
  1203. {
  1204. struct ipoib_neigh_table *ntbl = &priv->ntbl;
  1205. struct ipoib_neigh_hash *htbl;
  1206. unsigned long flags;
  1207. int i, wait_flushed = 0;
  1208. init_completion(&priv->ntbl.flushed);
  1209. spin_lock_irqsave(&priv->lock, flags);
  1210. htbl = rcu_dereference_protected(ntbl->htbl,
  1211. lockdep_is_held(&priv->lock));
  1212. if (!htbl)
  1213. goto out_unlock;
  1214. wait_flushed = atomic_read(&priv->ntbl.entries);
  1215. if (!wait_flushed)
  1216. goto free_htbl;
  1217. for (i = 0; i < htbl->size; i++) {
  1218. struct ipoib_neigh *neigh;
  1219. struct ipoib_neigh __rcu **np = &htbl->buckets[i];
  1220. while ((neigh = rcu_dereference_protected(*np,
  1221. lockdep_is_held(&priv->lock))) != NULL) {
  1222. rcu_assign_pointer(*np,
  1223. rcu_dereference_protected(neigh->hnext,
  1224. lockdep_is_held(&priv->lock)));
  1225. /* remove from path/mc list */
  1226. list_del(&neigh->list);
  1227. call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
  1228. }
  1229. }
  1230. free_htbl:
  1231. rcu_assign_pointer(ntbl->htbl, NULL);
  1232. call_rcu(&htbl->rcu, neigh_hash_free_rcu);
  1233. out_unlock:
  1234. spin_unlock_irqrestore(&priv->lock, flags);
  1235. if (wait_flushed)
  1236. wait_for_completion(&priv->ntbl.flushed);
  1237. }
  1238. static void ipoib_neigh_hash_uninit(struct net_device *dev)
  1239. {
  1240. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1241. int stopped;
  1242. ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
  1243. init_completion(&priv->ntbl.deleted);
  1244. set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
  1245. /* Stop GC if called at init fail need to cancel work */
  1246. stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1247. if (!stopped)
  1248. cancel_delayed_work(&priv->neigh_reap_task);
  1249. ipoib_flush_neighs(priv);
  1250. wait_for_completion(&priv->ntbl.deleted);
  1251. }
  1252. int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
  1253. {
  1254. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1255. /* Allocate RX/TX "rings" to hold queued skbs */
  1256. priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
  1257. GFP_KERNEL);
  1258. if (!priv->rx_ring) {
  1259. printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
  1260. ca->name, ipoib_recvq_size);
  1261. goto out;
  1262. }
  1263. priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
  1264. if (!priv->tx_ring) {
  1265. printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
  1266. ca->name, ipoib_sendq_size);
  1267. goto out_rx_ring_cleanup;
  1268. }
  1269. /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
  1270. if (ipoib_ib_dev_init(dev, ca, port))
  1271. goto out_tx_ring_cleanup;
  1272. /*
  1273. * Must be after ipoib_ib_dev_init so we can allocate a per
  1274. * device wq there and use it here
  1275. */
  1276. if (ipoib_neigh_hash_init(priv) < 0)
  1277. goto out_dev_uninit;
  1278. return 0;
  1279. out_dev_uninit:
  1280. ipoib_ib_dev_cleanup(dev);
  1281. out_tx_ring_cleanup:
  1282. vfree(priv->tx_ring);
  1283. out_rx_ring_cleanup:
  1284. kfree(priv->rx_ring);
  1285. out:
  1286. return -ENOMEM;
  1287. }
  1288. void ipoib_dev_cleanup(struct net_device *dev)
  1289. {
  1290. struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
  1291. LIST_HEAD(head);
  1292. ASSERT_RTNL();
  1293. ipoib_delete_debug_files(dev);
  1294. /* Delete any child interfaces first */
  1295. list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
  1296. /* Stop GC on child */
  1297. set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
  1298. cancel_delayed_work(&cpriv->neigh_reap_task);
  1299. unregister_netdevice_queue(cpriv->dev, &head);
  1300. }
  1301. unregister_netdevice_many(&head);
  1302. /*
  1303. * Must be before ipoib_ib_dev_cleanup or we delete an in use
  1304. * work queue
  1305. */
  1306. ipoib_neigh_hash_uninit(dev);
  1307. ipoib_ib_dev_cleanup(dev);
  1308. kfree(priv->rx_ring);
  1309. vfree(priv->tx_ring);
  1310. priv->rx_ring = NULL;
  1311. priv->tx_ring = NULL;
  1312. }
  1313. static const struct header_ops ipoib_header_ops = {
  1314. .create = ipoib_hard_header,
  1315. };
  1316. static const struct net_device_ops ipoib_netdev_ops = {
  1317. .ndo_uninit = ipoib_uninit,
  1318. .ndo_open = ipoib_open,
  1319. .ndo_stop = ipoib_stop,
  1320. .ndo_change_mtu = ipoib_change_mtu,
  1321. .ndo_fix_features = ipoib_fix_features,
  1322. .ndo_start_xmit = ipoib_start_xmit,
  1323. .ndo_tx_timeout = ipoib_timeout,
  1324. .ndo_set_rx_mode = ipoib_set_mcast_list,
  1325. .ndo_get_iflink = ipoib_get_iflink,
  1326. };
  1327. void ipoib_setup(struct net_device *dev)
  1328. {
  1329. struct ipoib_dev_priv *priv = netdev_priv(dev);
  1330. dev->netdev_ops = &ipoib_netdev_ops;
  1331. dev->header_ops = &ipoib_header_ops;
  1332. ipoib_set_ethtool_ops(dev);
  1333. netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
  1334. dev->watchdog_timeo = HZ;
  1335. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  1336. dev->hard_header_len = IPOIB_ENCAP_LEN;
  1337. dev->addr_len = INFINIBAND_ALEN;
  1338. dev->type = ARPHRD_INFINIBAND;
  1339. dev->tx_queue_len = ipoib_sendq_size * 2;
  1340. dev->features = (NETIF_F_VLAN_CHALLENGED |
  1341. NETIF_F_HIGHDMA);
  1342. netif_keep_dst(dev);
  1343. memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
  1344. priv->dev = dev;
  1345. spin_lock_init(&priv->lock);
  1346. init_rwsem(&priv->vlan_rwsem);
  1347. INIT_LIST_HEAD(&priv->path_list);
  1348. INIT_LIST_HEAD(&priv->child_intfs);
  1349. INIT_LIST_HEAD(&priv->dead_ahs);
  1350. INIT_LIST_HEAD(&priv->multicast_list);
  1351. INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
  1352. INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
  1353. INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
  1354. INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
  1355. INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
  1356. INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
  1357. INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
  1358. INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
  1359. }
  1360. struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
  1361. {
  1362. struct net_device *dev;
  1363. dev = alloc_netdev((int)sizeof(struct ipoib_dev_priv), name,
  1364. NET_NAME_UNKNOWN, ipoib_setup);
  1365. if (!dev)
  1366. return NULL;
  1367. return netdev_priv(dev);
  1368. }
  1369. static ssize_t show_pkey(struct device *dev,
  1370. struct device_attribute *attr, char *buf)
  1371. {
  1372. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  1373. return sprintf(buf, "0x%04x\n", priv->pkey);
  1374. }
  1375. static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
  1376. static ssize_t show_umcast(struct device *dev,
  1377. struct device_attribute *attr, char *buf)
  1378. {
  1379. struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
  1380. return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
  1381. }
  1382. void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
  1383. {
  1384. struct ipoib_dev_priv *priv = netdev_priv(ndev);
  1385. if (umcast_val > 0) {
  1386. set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  1387. ipoib_warn(priv, "ignoring multicast groups joined directly "
  1388. "by userspace\n");
  1389. } else
  1390. clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
  1391. }
  1392. static ssize_t set_umcast(struct device *dev,
  1393. struct device_attribute *attr,
  1394. const char *buf, size_t count)
  1395. {
  1396. unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
  1397. ipoib_set_umcast(to_net_dev(dev), umcast_val);
  1398. return count;
  1399. }
  1400. static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
  1401. int ipoib_add_umcast_attr(struct net_device *dev)
  1402. {
  1403. return device_create_file(&dev->dev, &dev_attr_umcast);
  1404. }
  1405. static ssize_t create_child(struct device *dev,
  1406. struct device_attribute *attr,
  1407. const char *buf, size_t count)
  1408. {
  1409. int pkey;
  1410. int ret;
  1411. if (sscanf(buf, "%i", &pkey) != 1)
  1412. return -EINVAL;
  1413. if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
  1414. return -EINVAL;
  1415. /*
  1416. * Set the full membership bit, so that we join the right
  1417. * broadcast group, etc.
  1418. */
  1419. pkey |= 0x8000;
  1420. ret = ipoib_vlan_add(to_net_dev(dev), pkey);
  1421. return ret ? ret : count;
  1422. }
  1423. static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
  1424. static ssize_t delete_child(struct device *dev,
  1425. struct device_attribute *attr,
  1426. const char *buf, size_t count)
  1427. {
  1428. int pkey;
  1429. int ret;
  1430. if (sscanf(buf, "%i", &pkey) != 1)
  1431. return -EINVAL;
  1432. if (pkey < 0 || pkey > 0xffff)
  1433. return -EINVAL;
  1434. ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
  1435. return ret ? ret : count;
  1436. }
  1437. static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
  1438. int ipoib_add_pkey_attr(struct net_device *dev)
  1439. {
  1440. return device_create_file(&dev->dev, &dev_attr_pkey);
  1441. }
  1442. int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
  1443. {
  1444. struct ib_device_attr *device_attr;
  1445. int result = -ENOMEM;
  1446. device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
  1447. if (!device_attr) {
  1448. printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
  1449. hca->name, sizeof *device_attr);
  1450. return result;
  1451. }
  1452. result = ib_query_device(hca, device_attr);
  1453. if (result) {
  1454. printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
  1455. hca->name, result);
  1456. kfree(device_attr);
  1457. return result;
  1458. }
  1459. priv->hca_caps = device_attr->device_cap_flags;
  1460. kfree(device_attr);
  1461. if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
  1462. priv->dev->hw_features = NETIF_F_SG |
  1463. NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  1464. if (priv->hca_caps & IB_DEVICE_UD_TSO)
  1465. priv->dev->hw_features |= NETIF_F_TSO;
  1466. priv->dev->features |= priv->dev->hw_features;
  1467. }
  1468. return 0;
  1469. }
  1470. static struct net_device *ipoib_add_port(const char *format,
  1471. struct ib_device *hca, u8 port)
  1472. {
  1473. struct ipoib_dev_priv *priv;
  1474. struct ib_port_attr attr;
  1475. int result = -ENOMEM;
  1476. priv = ipoib_intf_alloc(format);
  1477. if (!priv)
  1478. goto alloc_mem_failed;
  1479. SET_NETDEV_DEV(priv->dev, hca->dma_device);
  1480. priv->dev->dev_id = port - 1;
  1481. result = ib_query_port(hca, port, &attr);
  1482. if (!result)
  1483. priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
  1484. else {
  1485. printk(KERN_WARNING "%s: ib_query_port %d failed\n",
  1486. hca->name, port);
  1487. goto device_init_failed;
  1488. }
  1489. /* MTU will be reset when mcast join happens */
  1490. priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
  1491. priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
  1492. priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
  1493. result = ib_query_pkey(hca, port, 0, &priv->pkey);
  1494. if (result) {
  1495. printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
  1496. hca->name, port, result);
  1497. goto device_init_failed;
  1498. }
  1499. result = ipoib_set_dev_features(priv, hca);
  1500. if (result)
  1501. goto device_init_failed;
  1502. /*
  1503. * Set the full membership bit, so that we join the right
  1504. * broadcast group, etc.
  1505. */
  1506. priv->pkey |= 0x8000;
  1507. priv->dev->broadcast[8] = priv->pkey >> 8;
  1508. priv->dev->broadcast[9] = priv->pkey & 0xff;
  1509. result = ib_query_gid(hca, port, 0, &priv->local_gid);
  1510. if (result) {
  1511. printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
  1512. hca->name, port, result);
  1513. goto device_init_failed;
  1514. } else
  1515. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  1516. result = ipoib_dev_init(priv->dev, hca, port);
  1517. if (result < 0) {
  1518. printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
  1519. hca->name, port, result);
  1520. goto device_init_failed;
  1521. }
  1522. INIT_IB_EVENT_HANDLER(&priv->event_handler,
  1523. priv->ca, ipoib_event);
  1524. result = ib_register_event_handler(&priv->event_handler);
  1525. if (result < 0) {
  1526. printk(KERN_WARNING "%s: ib_register_event_handler failed for "
  1527. "port %d (ret = %d)\n",
  1528. hca->name, port, result);
  1529. goto event_failed;
  1530. }
  1531. result = register_netdev(priv->dev);
  1532. if (result) {
  1533. printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
  1534. hca->name, port, result);
  1535. goto register_failed;
  1536. }
  1537. ipoib_create_debug_files(priv->dev);
  1538. if (ipoib_cm_add_mode_attr(priv->dev))
  1539. goto sysfs_failed;
  1540. if (ipoib_add_pkey_attr(priv->dev))
  1541. goto sysfs_failed;
  1542. if (ipoib_add_umcast_attr(priv->dev))
  1543. goto sysfs_failed;
  1544. if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
  1545. goto sysfs_failed;
  1546. if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
  1547. goto sysfs_failed;
  1548. return priv->dev;
  1549. sysfs_failed:
  1550. ipoib_delete_debug_files(priv->dev);
  1551. unregister_netdev(priv->dev);
  1552. register_failed:
  1553. ib_unregister_event_handler(&priv->event_handler);
  1554. flush_workqueue(ipoib_workqueue);
  1555. /* Stop GC if started before flush */
  1556. set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1557. cancel_delayed_work(&priv->neigh_reap_task);
  1558. flush_workqueue(priv->wq);
  1559. event_failed:
  1560. ipoib_dev_cleanup(priv->dev);
  1561. device_init_failed:
  1562. free_netdev(priv->dev);
  1563. alloc_mem_failed:
  1564. return ERR_PTR(result);
  1565. }
  1566. static void ipoib_add_one(struct ib_device *device)
  1567. {
  1568. struct list_head *dev_list;
  1569. struct net_device *dev;
  1570. struct ipoib_dev_priv *priv;
  1571. int p;
  1572. int count = 0;
  1573. dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
  1574. if (!dev_list)
  1575. return;
  1576. INIT_LIST_HEAD(dev_list);
  1577. for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
  1578. if (!rdma_protocol_ib(device, p))
  1579. continue;
  1580. dev = ipoib_add_port("ib%d", device, p);
  1581. if (!IS_ERR(dev)) {
  1582. priv = netdev_priv(dev);
  1583. list_add_tail(&priv->list, dev_list);
  1584. count++;
  1585. }
  1586. }
  1587. if (!count) {
  1588. kfree(dev_list);
  1589. return;
  1590. }
  1591. ib_set_client_data(device, &ipoib_client, dev_list);
  1592. }
  1593. static void ipoib_remove_one(struct ib_device *device, void *client_data)
  1594. {
  1595. struct ipoib_dev_priv *priv, *tmp;
  1596. struct list_head *dev_list = client_data;
  1597. if (!dev_list)
  1598. return;
  1599. list_for_each_entry_safe(priv, tmp, dev_list, list) {
  1600. ib_unregister_event_handler(&priv->event_handler);
  1601. flush_workqueue(ipoib_workqueue);
  1602. rtnl_lock();
  1603. dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
  1604. rtnl_unlock();
  1605. /* Stop GC */
  1606. set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
  1607. cancel_delayed_work(&priv->neigh_reap_task);
  1608. flush_workqueue(priv->wq);
  1609. unregister_netdev(priv->dev);
  1610. free_netdev(priv->dev);
  1611. }
  1612. kfree(dev_list);
  1613. }
  1614. static int __init ipoib_init_module(void)
  1615. {
  1616. int ret;
  1617. ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
  1618. ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
  1619. ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
  1620. ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
  1621. ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
  1622. ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
  1623. #ifdef CONFIG_INFINIBAND_IPOIB_CM
  1624. ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
  1625. #endif
  1626. /*
  1627. * When copying small received packets, we only copy from the
  1628. * linear data part of the SKB, so we rely on this condition.
  1629. */
  1630. BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
  1631. ret = ipoib_register_debugfs();
  1632. if (ret)
  1633. return ret;
  1634. /*
  1635. * We create a global workqueue here that is used for all flush
  1636. * operations. However, if you attempt to flush a workqueue
  1637. * from a task on that same workqueue, it deadlocks the system.
  1638. * We want to be able to flush the tasks associated with a
  1639. * specific net device, so we also create a workqueue for each
  1640. * netdevice. We queue up the tasks for that device only on
  1641. * its private workqueue, and we only queue up flush events
  1642. * on our global flush workqueue. This avoids the deadlocks.
  1643. */
  1644. ipoib_workqueue = create_singlethread_workqueue("ipoib_flush");
  1645. if (!ipoib_workqueue) {
  1646. ret = -ENOMEM;
  1647. goto err_fs;
  1648. }
  1649. ib_sa_register_client(&ipoib_sa_client);
  1650. ret = ib_register_client(&ipoib_client);
  1651. if (ret)
  1652. goto err_sa;
  1653. ret = ipoib_netlink_init();
  1654. if (ret)
  1655. goto err_client;
  1656. return 0;
  1657. err_client:
  1658. ib_unregister_client(&ipoib_client);
  1659. err_sa:
  1660. ib_sa_unregister_client(&ipoib_sa_client);
  1661. destroy_workqueue(ipoib_workqueue);
  1662. err_fs:
  1663. ipoib_unregister_debugfs();
  1664. return ret;
  1665. }
  1666. static void __exit ipoib_cleanup_module(void)
  1667. {
  1668. ipoib_netlink_fini();
  1669. ib_unregister_client(&ipoib_client);
  1670. ib_sa_unregister_client(&ipoib_sa_client);
  1671. ipoib_unregister_debugfs();
  1672. destroy_workqueue(ipoib_workqueue);
  1673. }
  1674. module_init(ipoib_init_module);
  1675. module_exit(ipoib_cleanup_module);